How to insert checkbox values into table using PHP?

Vivianne Daniels

Anyway, I have a form for registering volunteers - normal stuff, it all passes. But then I have a 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?

Update/edit: weird typo at statements2 and 3 and another typo - but no solution to the problem though. Changing the bindparam(':t_id',$task, PDO::PARAM_INT) to bindparam(':t_id',$task, PDO::PARAM_STR) doesn't work either (although I know the problem is probably somewhere in that statement).

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://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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_id)"); 
        $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_id)"); 
        $statement3->bindparam(':t_id',$task, PDO::PARAM_INT);
        $statement3->bindparam(':id',$last_id, PDO::PARAM_INT);
        foreach ($_POST['tid'] as $task) {$statement3->execute(); }


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

}
}
david strachan

You require to bind parameter for each loop

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


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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to insert checkbox values into table using PHP?

From Dev

how to insert multiple checkbox values in single column in database using php

From Dev

Insert values into MySQL table using PHP

From Dev

How do I insert multiple checkbox values into a table?

From Dev

how to insert values in table in cake php

From Dev

How to insert two different check box values into different columns of one mysql table using php?

From Dev

How to insert sandbox Paypal return values into MySQL table using PHP Wordpress?

From Dev

How to insert values in table

From Dev

how to get from two table checked checkbox values using jquery?

From Dev

how to use checkbox values to access database using php?

From Dev

How to send checkbox values in contact form using php?

From Dev

How to set values in checkbox using php array and Mysql

From Dev

insert values into table using Varray

From Dev

Laravel : How to insert checkbox values with submit button?

From Dev

How to show table values in php using PDO

From Dev

how to insert two values manually into table by using code

From Dev

How to insert values into the database table using VBA in MS access

From Dev

how to insert values from an array into a table in oracle using delimiter

From Dev

how to insert/delete values from table using colum it does not have

From Dev

How to insert multiple values in a specific field in a table using mysqli?

From Dev

How correctly insert values in table using Doctrine ORM?

From Dev

How to get insert values from a table into an array using SQL?

From Dev

how to insert values in an sql table with uniqueidentifier column using vb?

From Dev

How to use the checkbox in a table php

From Dev

How to insert values into a junction table?

From Dev

how insert array values into table

From Dev

How to insert values into a junction table?

From Dev

How to insert data into db using checkbox using ajax and php without form

From Dev

How to insert values of dynamic fields into database using PHP ,MySqL and JQuery

Related Related

  1. 1

    How to insert checkbox values into table using PHP?

  2. 2

    how to insert multiple checkbox values in single column in database using php

  3. 3

    Insert values into MySQL table using PHP

  4. 4

    How do I insert multiple checkbox values into a table?

  5. 5

    how to insert values in table in cake php

  6. 6

    How to insert two different check box values into different columns of one mysql table using php?

  7. 7

    How to insert sandbox Paypal return values into MySQL table using PHP Wordpress?

  8. 8

    How to insert values in table

  9. 9

    how to get from two table checked checkbox values using jquery?

  10. 10

    how to use checkbox values to access database using php?

  11. 11

    How to send checkbox values in contact form using php?

  12. 12

    How to set values in checkbox using php array and Mysql

  13. 13

    insert values into table using Varray

  14. 14

    Laravel : How to insert checkbox values with submit button?

  15. 15

    How to show table values in php using PDO

  16. 16

    how to insert two values manually into table by using code

  17. 17

    How to insert values into the database table using VBA in MS access

  18. 18

    how to insert values from an array into a table in oracle using delimiter

  19. 19

    how to insert/delete values from table using colum it does not have

  20. 20

    How to insert multiple values in a specific field in a table using mysqli?

  21. 21

    How correctly insert values in table using Doctrine ORM?

  22. 22

    How to get insert values from a table into an array using SQL?

  23. 23

    how to insert values in an sql table with uniqueidentifier column using vb?

  24. 24

    How to use the checkbox in a table php

  25. 25

    How to insert values into a junction table?

  26. 26

    how insert array values into table

  27. 27

    How to insert values into a junction table?

  28. 28

    How to insert data into db using checkbox using ajax and php without form

  29. 29

    How to insert values of dynamic fields into database using PHP ,MySqL and JQuery

HotTag

Archive