检查从数据库表中检索的复选框值并将其传输到同一数据库中的另一个表

时间:2015-01-24 13:41:34

标签: mysql

我正在努力做一些重要的事情 - 我已经有一个复选框列表 - 复选框列表的值是从数据库中检索的 - 所以它没有手动输入 - 我试图选择一个随机的一组列表中的值我必须勾选 - 但问题是我无法将它们插入到mysql数据库中 - 所以我将提供带有复选框列表的表格的屏幕截图然后复制并粘贴下面的必要代码=

enter image description here

检索两个圆柱表的数据的代码是:

    <table border="1" cellspacing="0" style="margin-top:12px width="900"">
    <colgroup>
        <col span="1">
    </colgroup>
<tr>
    <td>
        Engineering Services
    </td>
    <td>
        Information Technologies
    </td>
    <tr>
        <td valign="top">
            <?php

            $list = "SELECT * FROM specifications WHERE category='Engineering' ORDER BY specifications ASC";
        //$list = "SELECT * FROM specifications ";
        //$list1 = "SELECT * FROM specifications ORDER BY category ASC Limit 2";
            $listAHI = $dbs ->prepare($list);
            $listAHI -> execute();

        //while($row1 = $listAHI1 ->fetch(PDO::FETCH_ASSOC)){
            while($row = $listAHI ->fetch(PDO::FETCH_ASSOC))
            {
                $specifications = $row["specifications"];
                $category = $row["category"];
                echo "
                <input type='checkbox' name='$specifications'[] value='$specifications'>  $specifications<br> 
                ";
            }
            ?>
        </td>
        <td valign="top">
            <?php
            $list = "SELECT * FROM specifications WHERE category='Information' ORDER BY specifications ASC";
        //$list = "SELECT * FROM specifications ";
        //$list1 = "SELECT * FROM specifications ORDER BY category ASC Limit 2";
            $listAHI = $dbs ->prepare($list);
            $listAHI -> execute();

        //while($row1 = $listAHI1 ->fetch(PDO::FETCH_ASSOC)){
            while($row = $listAHI ->fetch(PDO::FETCH_ASSOC))
            {
                $specifications = $row["specifications"];
                $category = $row["category"];

                echo "
                <input type='checkbox' name='$specifications'[] value='$specifications'>  $specifications<br> 
                ";
            }

            ?>
        </td>
    </tr>
</table>

将复选框插入数据库的代码也粘贴在下面:

<?php
//session_start();
include('dbcategory.php');
//include('insertdata3.php');
error_reporting(0);
error_reporting(E_ERROR | E_PARSE);

$first_name = $middle_name = $last_name = $street = $unit = $city = $region = $postalcode = $specifications="";
$businessnumbercountry =  $businessnumbercode =  $businessnumber =  $phone =  $phonecountry = $cellphonearea = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $id = $_POST["id"];
    $first_name = $_POST["first_name"];
    $middle_names = $_POST["middle_names"];
    $last_name = $_POST["last_name"];
    $street = $_POST["street"];
    $unit = $_POST["unit"];
    $city = $_POST["city"];
    $region = $_POST["region"];
    $postalcode = $_POST["postalcode"];
    $country = $_POST["country"];
    $email = $_POST["email"];

    $businessnumber = $_POST["businessnumber"];
    $businessnumbercountry = $_POST["businessnumbercountry"];
    $businessnumberarea = $_POST["businessnumberarea"];

    $phone = $_POST["phone"];
    $phonearea = $_POST["phonearea"];
    $phonecountry = $_POST["phonecountry"];
    $cellphonecountry = $_POST["cellphonecountry"];
    $cellphonearea = $_POST["cellphonearea"];

    $cellphone = $_POST["cellphone"];
//$specifications = $_POST["specifications"];
    $specifications = implode(",",$_POST['specifications']);
    echo $specifications;
}


if (isset($_POST["submitd"]) && $_POST["submitd"] == "Apply" ){
    $specifications = implode(",",$_POST['specifications']);
    echo $specifications;
    $query567="INSERT into emprecords(id, first_name, middle_names, last_name, street, unit, city, region, postalcode, country, email, businessnumbercountry, businessnumberarea, businessnumber, phonecountry, phonearea, phone, cellphonecountry, cellphonearea, cellphone, specifications) VALUES('$id', '$first_name', '$middle_names', '$last_name', '$street', '$unit', '$city', '$region', '$postalcode', '$country', '$email', '$businessnumbercountry','$businessnumberarea','$businessnumber', '$phonecountry', '$phonearea', '$phone', '$cellphonecountry', '$cellphonearea','$cellphone','$specifications');";
    $vttvk = $dbs -> prepare($query567);
    $vttvk -> execute(array(':id'=>$id,':first_name'=>$first_name,':middle_names'=>$middle_names,':last_name'=>$last_name,':street'=>$street,':unit'=>$unit,':city'=>$city,':region'=>$region,':postalcode'=>$postalcode,':country'=>$country,':email'=>$email, ':businessnumbercountry'=>$businessnumbercountry, ':businessnumberarea'=>$businessnumberarea, ':businessnumber'=>$businessnumber,':phonecountry'=>$phonecountry, ':phonearea'=>$phonearea, ':phone'=>$phone, ':cellphonecountry'=>$cellphonecountry, ':cellphonearea'=>$cellphonearea, ':cellphone'=>$cellphone, ':specifications'=>$specifications));

} 
?>

请帮我弄清楚如何使用php将多个复选框值插入mysql数据库 - 谢谢!我已经有一个数组用于打印出我从表中检索到的数据库,然后在我尝试将其插入另一个mysql表时插入数组...其中我出错了?

1 个答案:

答案 0 :(得分:0)

您的复选框名称为$specifications[],它将转换为该变量,每个复选框都有一个唯一的名称,而不是创建一个名为specifications的数组。 因此,如果数据库中的某个规范是test1,另一个规范是test2,那么您的代码为:

<input type='checkbox' name='$specifications'[] value='$specifications'>  $specifications<br>

将呈现为

<input type='checkbox' name='test1'[] value='test1'> test1<br> 
<input type='checkbox' name='test2'[] value='test2'> test2<br> 

这将为您提供$_POST[test1]='test1'$_POST[test2]='test2'

取消复选框名称中的$,如下所示

<input type='checkbox' name='specifications'[] value='$specifications'>  $specifications<br>

将呈现为

<input type='checkbox' name='specifications'[] value='test1'> test1<br> 
<input type='checkbox' name='specifications'[] value='test2'> test2<br>