Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正在写入的上一行值_Php_Sql_Arrays_Foreach - Fatal编程技术网

Php 正在写入的上一行值

Php 正在写入的上一行值,php,sql,arrays,foreach,Php,Sql,Arrays,Foreach,我试图只从名为“Addbox”的复选框为true的行中添加值,下面的代码标识何时勾选复选框,并且只将这些项目添加到用户愿望列表中。但是,无论复选框为true还是false,它都会将它们与前一行的数量一起添加 复选框代码为 "input type = 'checkbox' name='Addbox[]' value='" . $row['Item_ID'] . "' 如果语句代码为 if (isset($_POST['Add'])) { if(isset ($_POST['Addbox']

我试图只从名为“Addbox”的复选框为true的行中添加值,下面的代码标识何时勾选复选框,并且只将这些项目添加到用户愿望列表中。但是,无论复选框为true还是false,它都会将它们与前一行的数量一起添加

复选框代码为

"input type = 'checkbox' name='Addbox[]' value='" . $row['Item_ID'] . "'
如果语句代码为

if (isset($_POST['Add'])) {

  if(isset ($_POST['Addbox'])){

    //counts the size of the array addbox[] so the for loop has an end.
    $count = count($_POST['Addbox']);

    //starts the sql query string to added to inside the for loop.
    $sql = "INSERT INTO `wishlist` (`User_ID`,`Product_ID`,`Quantity`) VALUES ";

    //initiate for loop... do this until every row in the array has been looked at essentially.
    for ($i=0; $i < $count; $i++) {

        //get the values of this row.
        $item = $_POST['Addbox'][$i];
        $quantity = $_POST['quantity'][$i];

        // Add the end of the sql string for each of the loops.
        $sql .= "($userid, $item, $quantity)";

        //This check is it is the last of the row in the array. If it isnt add a comma and space ready for the next row.
        if($i !== $count-1){
            $sql .= ", ";
        }
    }

    mysql_query($sql);
    // var_dump($sql)
}
if(isset($\u POST['Add'])){
如果(isset($_POST['Addbox'])){
//计算数组addbox[]的大小,以便for循环有一个结束。
$count=count($_POST['Addbox']);
//启动要添加到for循环内部的sql查询字符串。
$sql=“插入到“愿望列表”('User\u ID`、'Product\u ID`、'Quantity`)值中”;
//启动for循环…执行此操作,直到数组中的每一行基本上都已查看。
对于($i=0;$i<$count;$i++){
//获取此行的值。
$item=$_POST['Addbox'][$i];
$quantity=$_POST['quantity'][$i];
//为每个循环添加sql字符串的结尾。
$sql.=“($userid,$item,$quantity)”;
//此检查为,它是数组中的最后一行。如果不是,请为下一行添加逗号和空格。
如果($i!==$count-1){
$sql.=“,”;
}
}
mysql_查询($sql);
//变量转储($sql)
}
试试这个:

foreach($_POST['Addbox'] as $key=>$itemId){
    //get the values of this row.
    $item = $_POST['Addbox'][$key];
    $quantity = if (!empty($_POST['quantity'][$key])) $_POST['quantity'][$key]; else 0;
    // Could also be written as $quantity = !empty($_POST['quantity'][$key]) ? $_POST['quantity'][$key] : 0;

    // Add the end of the sql string for each of the loops.
    $sql .= "($userid, $item, $quantity)";
}
对于HTML,请尝试以下操作:

"<input type='checkbox' name='Addbox[{$row['Item_ID']}]' value='{$row['Item_ID']}' />"
“”
这将遍历数组中的每个项
$\u POST['Addbox']
,并为您提供数组中的
$key
,以及项的
$itemId
,或值(您创建了项ID)。然后,它将附加到SQL字符串中

警告:您通常希望通过健全性和有效性检查来防止恶意客户端。StackOverflow上已经有了一些很好的答案,可以防止SQL注入。您还需要检查是否有空的、无效的字符、溢出的数字等。目前这个代码段很容易受到攻击,可能会导致问题如果
$key
$itemId
包含意外值(包括未设置或为空)


有关
foreach()的更多信息

我会更担心你的代码中的漏洞……但请记住,表单中未勾选的复选框不会提交,因此你收到的唯一复选框是选中的。mysql…我们又来了。这是大学学位的一部分,所以mysql是我唯一的选择,因为它是标准的一部分。如果K未提交复选框如何停止每次写入$quantity变量,而不仅仅是在复选框被选中时?我尝试了“请选择”选项,但由于它是一个数组,它只是将SQL语句全部打断。请尝试
print\r($\u POST)
并查看您拥有的值。这是一个SQL问题吗?它看起来像HTML问题或逻辑问题。@RickyHelp如果您阅读Arthur给您的链接,该MySQL API现在已被弃用,并且已经声明弃用了一段时间。还有其他MySQL API,如mysqli和PDO.read。如果您很强硬的话t使用旧的
mysql方法,你的大学正在给你带来伤害。恐怕这并不能解决问题,打印输出是>数组([quantity]=>Array([0]=>1[1]=>6[2]=>9)[Addbox]=>Array([0]=>2[1]=>39)[additem]=>将选定项添加到愿望列表。与之前一样,它会将数量的前两个值写入选定项。您的问题是您正在为未选中的项填写数量字段。请尝试将HTML更改为包含
name='Addbox[0]'
name='quantity[0]“
,为每个连续项目递增0。然后,给定项目的键将与复选框和项目数量匹配。(
Addbox[1]
将对应于
quantity[1]
,如果两者都已设置)。否则,
Addbox[0]
可能对应于第五个文本框,如果这是第一个已设置的文本框(第一个到第四个文本框为空)。更改了html,以便选择name=quantity[0]和input type=checkbox name addbox[0],现在在选中两个框后,我的打印输出是Array([quantity]=>Array([0]=>6)[addbox]=>Array([0]=>39)[additem]=>将所选内容添加到愿望列表