【问题标题】:Adding multiple check box values to a database向数据库添加多个复选框值
【发布时间】:2014-08-14 00:29:49
【问题描述】:

我正在尝试制作表格,让用户在他们吃过的食物的方框中打勾,并将碳水化合物值发送到另一张桌子。表的内容在我用以下代码调用它的数据库中:

while($row = mysqli_fetch_array($q))
    {

    echo "<form action='add.php' method='post' id='add'><tr>";
    echo "<td>".$row['carb_id']."</td>";
    echo "<td>".$row['food_item']."</td>";
    echo "<td>".$row['serving_size']."</td>";
    echo "<td>".$row['carbs_per_serving']."</td>";
    echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."'
 name='food[]'></td>";
         echo "</tr></form>";

                 }
        echo "<input type='submit' class='add'form='add'>";

添加到另一个表的PHP是:

<?php
        /*blog.php
            process a basic form saving data
        */

    error_reporting ('E_all');

    //Create a connection to the database
    $link = mysqli_connect('localhost','root','','login') 
        or die('Error' . mysqli_error($link));

    //If there is no connection trhows up an error message
    if (mysqli_connect_errno())
        {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

    $food = $_POST['food'];


    //Insert values into the database       
        //mysqli_query($link,"INSERT INTO blog(post)
        //  VALUES ('".$post."')"
        //  );

        //echos out the information put in apart from the password
        mysqli_query($link,"INSERT INTO carbsummary(cpp)
                        VALUES ('".$food."')"
                        );
            echo $food;
    ?>

我希望我在这里正确地解释了自己。简而言之,我希望用户选中这些框,然后将食物的碳水化合物值提交到下一张表。

我对编码不是很擅长,因为我刚开始不久,但我似乎无法在任何地方找到解决方案。

【问题讨论】:

  • 您很容易受到SQL injection attacks 的攻击,并且您正在尝试将数组插入到查询字符串中。 $food 是一个数组,而不是一个字符串。

标签: php html mysql checkbox html-table


【解决方案1】:

第一:

当您创建表单时,您的循环将创建许多表单。所以你应该改变这个代码:

    while($row = mysqli_fetch_array($q)){
    echo "<form action='add.php' method='post' id='add'><tr>";
    echo "<td>".$row['carb_id']."</td>";
    echo "<td>".$row['food_item']."</td>";
    echo "<td>".$row['serving_size']."</td>";
    echo "<td>".$row['carbs_per_serving']."</td>";
    echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."'
 name='food[]'></td>";
    echo "</tr></form>";
                 }
        echo "<input type='submit' class='add'form='add'>";

有了这个:

    while($row = mysqli_fetch_array($q)){
        echo "<form action='add.php' method='post' id='add'>";
        echo "<tr><td>".$row['carb_id']."</td>";
        echo "<td>".$row['food_item']."</td>";
        echo "<td>".$row['serving_size']."</td>";
        echo "<td>".$row['carbs_per_serving']."</td>";
        echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."'
 name='food[]'></td>";                              }
        echo "<input type='submit' class='add'form='add'>";
        echo "</tr></form>";

}

第二:

$_POST['food'] 包含一个数组,因此它将返回一个数组,因此您应该调用该值的索引。

$food = $_POST['food'][0];

第三

如果您想同时发送多个插入,您应该更改您的表单:

echo "<form action='add.php' method='post' id='add'>"; 
while($row = mysqli_fetch_array($q)){                
            echo "<tr><td>".$row['carb_id']."</td>";
            echo "<td>".$row['food_item']."</td>";
            echo "<td>".$row['serving_size']."</td>";
            echo "<td>".$row['carbs_per_serving']."</td>";
            echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."'
     name='food[]'></td>";                              }
            echo "</tr>";
}
    echo "<input type='submit' class='add'form='add'></form>";

然后更改您的查询执行:

foreach($_POST["food"] AS $food){
     mysqli_query($link,"INSERT INTO carbsummary (cpp) VALUES ('{$food}')";
     echo $food;
}

【讨论】:

    【解决方案2】:
    1. 首先,您必须对所有传入 SQL 服务器的数据使用 mysql_real_escape_string()

    2. 您正在使用相当奇怪的属性 - form="add"。这不是一个粗鲁的错误,但如果你删除所有无用的代码会更好。

    3. 如果要将值保存到 db,则需要将从客户端获取的数组转换为字符串。这不是最好的解决方案,但您可以第一次使用implode 函数。

    改变

    $food=$_POST['food'];
    

    $food=implode(',',mysql_real_escape_string($_POST['food']);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多