lundi 4 avril 2016

Checkboxes values: How to insert them into table using php

I've been stuck on this for longer than I like now. I am not new, but it has been a long time and well somehow it just doesn't click at the moment (so much has changed!):P Anyway, I have a form for registering volunteers - normal stuff, it all passes But then the checkbox problem. Volunteers can help at different festivals and can be available for tasks (2 tables, where the id of the volunteer corresponds to id of festival/task). The code now is incomplete because I kept just getting errors *latest being: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

vardump results: array(2) { [0]=> string(1) "8" [1]=> string(2) "10" } array(4) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4"

Can anyone help me to get me on the right track so I can move on?

addvolunt.php

     <?php
    error_reporting(-1);
    ini_set('display_errors', 'On');
    if(!isset($_SESSION)){
    session_start();
    }
    include_once 'dbConnect.php';

    if(!isset($_SESSION['user']))
    {
        header("Location: index.php");
    }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ift.tt/kkyg93">
<html xmlns="http://ift.tt/lH0Osb">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $_SESSION['user']; ?></title>
 <link rel="stylesheet" type="text/css" href="../css/main.css" />
 <link rel="stylesheet" type="text/css" href="../css/menu.css" />
</head>
<body>
    <table class="center"><tr><td>
                    <p class="P1">Voeg hier een vrijwilliger toe</br>
                    <hr></hr>
                </p>
<?php
if(isset($_POST['reg_btn'])) {
    $p_fname = $_POST['fname'];
    $p_lname = $_POST['lname'];
    $p_dob = $_POST['dob'];
    $p_adres = $_POST['adres'];
    $p_pc = $_POST['pc'];
    $p_loc = $_POST['loc'];
    $p_coun = $_POST['coun'];
    $p_phone = $_POST['phone'];
    $p_phone2 = $_POST['phone2'];
    $p_mail = $_POST['mail'];

  if($volunt->createVolunt($p_fname,$p_lname,$p_dob,$p_adres,$p_pc,$p_loc,$p_coun,$p_phone,$p_phone2,$p_mail)) {;
        echo("Vrijwilliger toegevoegd");

    }

    else{
        echo("Registration Failed");
        var_dump($_POST['tid']);
        var_dump($_POST['fest']);
    }

}
?>
<div id="formdiv">    
<form method="post" id="form">
            <table class="center" width="30%" border="0">
                <tr>
                    <td>Voornaam</td><td><input type="text" name="fname" placeholder="Voornaam" required /></td>
                </tr>
                <tr>
                    <td>Achternaam</td><td><input type="text" name="lname" placeholder="Achternaam" required /></td>
                </tr>
                <tr>
                    <td>Geboortedatum dd/mm/jjjj</td><td><input type="text" name="dob" placeholder="Geboortedatum dd/mm/jjjj" required /></td>
                </tr>
                <tr>
                    <td>Adres</td><td><input type="text" name="adres" placeholder="Adres" required /></td>
                </tr>
                <tr>
                    <td>Postcode</td><td><input type="text" name="pc" placeholder="Postcode 1234AB" required /></td>
                </tr>
                  <tr>
                    <td>Plaats </td><td><input type="text" name="loc" placeholder="Plaats" required /></td>
                </tr>
                <tr>
                    <td>Land </td><td><input type="text" name="coun" placeholder="Land" required /></td>
                </tr>
                <tr>
                    <td> Mobiel</td><td><input type="text" name="phone" placeholder="Mobiel#" required /></td>
                </tr>
                <tr>
                    <td>Telefoon 2 </td><td><input type="text" name="phone2" placeholder="Vast#" required /></td>
                </tr>
                <tr>
                    <td>Email </td><td><input type="email" name="mail" placeholder="Email" required /></td>
                </tr>

                <table class="center"><th colspan="2">Festivals</th>
        <tr>
            <td><input type="checkbox" name="fest[]" value="1">Vurige Tongen</td>
          <td><input type="checkbox" name="fest[]" value="2">Solstice</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="fest[]" value="3">Landjuweel</td>
            <td><input type="checkbox" name="fest[]" value="4">Zaterdagen</td>
        </tr>
    </table>

                <table class="center"><th colspan="3">Taken</th>
        <tr>
            <td><input type="checkbox" name="tid[]" value="1">Bar</td>
             <td><input type="checkbox" name="tid[]" value="2">Camping</td>
            <td><input type="checkbox" name="tid[]" value="3">Hygiene</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="tid[]" value="4">Kassa</td>
            <td><input type="checkbox" name="tid[]" value="5">Keuken</td>
            <td><input type="checkbox" name="tid[]" value="6">Munt</td>
        </tr>
                    <tr>
            <td><input type="checkbox" name="tid[]" value="7">Parkeerbeheer</td>
            <td><input type="checkbox" name="tid[]" value="8">Vrijwilligerscoordinatie</td>
            <td><input type="checkbox" name="tid[]" value="9">Kantoor</td>
        </tr>
                    <tr>
            <td><input type="checkbox" name="tid[]" value="10">Runner</td>            
            <td><input type="checkbox" name="tid[]" value="11">Op/Afbouw</td>
            <td><input type="checkbox" name="tid[]" value="12">Techniek</td> 
        </tr>
    </table>

                <tr>
                    <td><button type="submit" name="reg_btn">Aanvallen</button></td>
                </tr>

            </table>
        </form>
</div>
</td></tr></table>
</body>
</html>

crud.php part

public function createVolunt($p_fname,$p_lname,$p_dob,$p_adres,$p_pc,$p_loc,$p_coun,$p_phone,$p_phone2,$p_mail){

    try{

        $statement = $this->Db->prepare("INSERT INTO RO_volunt(p_fname,p_lname,p_dob,p_adres,p_pc,p_loc,p_coun,p_phone,p_phone2,p_mail) VALUES (:p_fname,:p_lname,:p_dob,:p_adres,:p_pc,:p_loc,:p_coun,:p_phone,:p_phone2,:p_mail)") ; 
        $statement->bindparam(':p_fname',$p_fname, PDO::PARAM_STR);
        $statement->bindparam(':p_lname',$p_lname,PDO::PARAM_STR);
        $statement->bindparam(':p_dob',$p_dob,PDO::PARAM_STR);
        $statement->bindparam(':p_adres',$p_adres,PDO::PARAM_STR);
        $statement->bindparam(':p_pc',$p_pc,PDO::PARAM_STR);
        $statement->bindparam(':p_loc',$p_loc,PDO::PARAM_STR);
        $statement->bindparam(':p_coun',$p_coun,PDO::PARAM_STR);
        $statement->bindparam(':p_phone',$p_phone,PDO::PARAM_STR);
        $statement->bindparam(':p_phone2',$p_phone2,PDO::PARAM_STR);
        $statement->bindparam(':p_mail',$p_mail,PDO::PARAM_STR);
        $statement->execute();
        $last_id = $this->Db->lastInsertId();

        $statement2 = $this->Db->prepare("INSERT INTO RO_eventsvol(id,e_id) VALUES (:id,:e_id1)"); 
        $statement2->bindparam(':e_id',$fest, PDO::PARAM_INT);
        $statement2->bindparam(':id',$last_id, PDO::PARAM_INT);
        foreach ($_POST['fest'] as $fest) {$statement2->execute(); }

        $statement3 = $this->Db->prepare("INSERT INTO RO_tasksvol(id,t_id) VALUES (:id,:t_id1)"); 
        $statement3->bindparam(':t_id',$task, PDO::PARAM_INT);
        $statement2->bindparam(':id',$last_id, PDO::PARAM_INT);
        foreach ($_POST['tid'] as $task) {$statement3->execute(); }


    } catch (PDOException $ex){
        echo $ex->getMessage();
        return false;
    }

}

}




Aucun commentaire:

Enregistrer un commentaire