简体   繁体   English

使用PHP和MySQL从复选框保存数据

[英]Saving data from a checkbox using PHP and MySQL

Here is my code: 这是我的代码:

    while($row = mysqli_fetch_array($result))
{
    $id = $row['id'];
    $description = $row['description'];
    $picturepath = $row['picturepath'];
    $name = $row['name'];
    $price = $row['price'];
    $keywords = $row['keywords'];

    $dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
                        <tr height="20"></tr>
                        <tr>
                            <td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br />
                                <form id="form1" name="form1" method="post" action="cart.php">
                                    <input type="hidden" name="pid" id="pid" value=" ' . $name . ' &nbsp;&nbsp; - &nbsp;&nbsp; $' . $price . '&nbsp;&nbsp; - &nbsp;&nbsp; "checkbox"/>';


    echo $dynamiclist;

foreach(explode(',', $keywords) as $keyword) {
                    $keyword = trim($keyword);
                    echo "<input type='checkbox' name='checkbox' value='$keyword'>$keyword <br /><br />";
        }
        echo '<input type="submit" name="button" id="button" value="Add to Order"/> </form> </td></tr></table>';
}
mysqli_close($con); //close the db connection

The code pulls in an item using the items id, and then creates checkboxes and keywords for that item. 该代码使用项id提取项,然后为该项创建复选框和关键字。 A user is to click on the checkbox options they want and then submit the form which sends the information to the shopping cart. 用户将单击所需的复选框选项,然后提交将信息发送到购物车的表格。 The problem is that when the form is submitted, only the name and price of the item is sent over, but not the checkbox options that were clicked. 问题在于,提交表单时,仅发送商品的名称和价格,而不发送所单击的复选框选项。 Can you help me identify why? 您能帮我找出原因吗? Side note. 边注。 In my database I have a column "keywords" where multiple keywords are stored. 在我的数据库中,我有一列“关键字”,其中存储了多个关键字。 // medium, well, rare, milk, apple juice, fries. //中,好,稀有,牛奶,苹果汁,薯条。 Also, on the cart.php the I have: 另外,在cart.php上,我有:

if(isset($_POST['pid'])){
    $pid = $_POST['pid'];

which posts the data from the script using the checkboxes. 使用复选框从脚本中发布数据。 I'm just stuck and can't get any further. 我只是被困住了,无法继续前进。

You should have different name for your checkbox: 您的复选框应使用其他名称:

while($row = mysqli_fetch_array($result))
{
    $id = $row['id'];
    $description = $row['description'];
    $picturepath = $row['picturepath'];
    $name = $row['name'];
    $price = $row['price'];
    $keywords = $row['keywords'];

    $dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
                        <tr height="20"></tr>
                        <tr>
                            <td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br />
                                <form id="form1" name="form1" method="post" action="cart.php">
                                    <input type="hidden" name="pid" id="pid" value=" ' . $name . ' &nbsp;&nbsp; - &nbsp;&nbsp; $' . $price . '&nbsp;&nbsp; - &nbsp;&nbsp; "checkbox"/>';


    echo $dynamiclist;

$i=0;
foreach(explode(',', $keywords) as $keyword) {
    $keyword = trim($keyword);
    $chkname = "checkbox{$i}";
    $i = $i+1;
    echo "<input type='checkbox' name='$chkname' value='$keyword'>$keyword <br /><br />";

        }
        echo '<input type="submit" name="button" id="button" value="Add to Order"/> </form> </td></tr></table>';
}
mysqli_close($con); //close the db connection

You need to get checkbox data in the cart.php like this: 您需要像这样在cart.php中获取复选框数据:

  if($_REQUEST['checkbox0']) echo $_REQUEST['checkbox0'];
  if($_REQUEST['checkbox1']) echo $_REQUEST['checkbox1'];
   //so on foreach checkbox

Your checkboxes need to have different names, otherwise the values are overwritten: 您的复选框需要使用不同的名称,否则值将被覆盖:

<input type="checkbox" name="keyword1" value="keyword1">Keyword1
<input type="checkbox" name="keyword2" value="keyword2">Keyword2
<input type="checkbox" name="keyword3" value="keyword3">Keyword3

You then access them in PHP like so: 然后,您可以像这样在PHP中访问它们:

if ( isset($_GET['keyword1']) ) {

}
if ( isset($_GET['keyword2']) ) {

}
if ( isset($_GET['keyword3']) ) {

}

A more logical way to manage the data is to pass the checkbox values as an array: 管理数据的更合乎逻辑的方法是将复选框值作为数组传递:

<input type="checkbox" name="keyword[]" value="keyword1">Keyword1
<input type="checkbox" name="keyword[]" value="keyword2">Keyword2
<input type="checkbox" name="keyword[]" value="keyword3">Keyword3

It then becomes much easier to work with the data in PHP; 然后,使用PHP中的数据变得更加容易。

if ( isset($_GET['keyword']) && is_array($_GET['keyword']) ) {
    foreach ( $_GET['keyword'] as $v ) {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM