如何使用PHP将复选框值插入表中?

时间:2016-04-04 09:12:44

标签: php forms checkbox pdo sql-insert

无论如何,我有一张注册志愿者的表格 - 正常的东西,一切都过去了。但后来我遇到了一个复选框问题。志愿者可以在不同的节日中提供帮助,并且可以用于任务(2个表,其中志愿者的id对应于节日/任务的id)。 代码现在不完整,因为我一直在收到错误,最新的是:

  

SQLSTATE [HY093]:参数号无效:参数未定义

Vardump结果:

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" 

任何人都可以帮助我让我走上正轨让我继续前进吗?

更新/编辑:在语句2和3处发生了奇怪的拼写错误以及另一个错字 - 但是问题无法解决。将bindparam(':t_id',$task, PDO::PARAM_INT)更改为bindparam(':t_id',$task, PDO::PARAM_STR)也不起作用(虽然我知道问题可能在该声明中的某处)。

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 部分

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;
    }

}
}

3 个答案:

答案 0 :(得分:0)

您在查询2&amp ;;上错误输入了绑定的参数名称。 3

$statement2 = $this->Db->prepare("INSERT INTO RO_eventsvol(id,e_id) VALUES (:id,:e_id1)"); 
$statement2->bindparam(':e_id1',$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_id1',$task, PDO::PARAM_INT);
$statement2->bindparam(':id',$last_id, PDO::PARAM_INT);
foreach ($_POST['tid'] as $task) {$statement3->execute();

将修复它

答案 1 :(得分:0)

您需要为每个循环绑定参数

 $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();
        }

答案 2 :(得分:0)

(代表OP发布)

解决:这是具有相同问题的人的正确代码:

assert(size(threads) == size(partVec))