mysql查询id作为变量不起作用

时间:2017-07-31 15:38:07

标签: php mysql

我想更新我的MySQL表。当我输入ID作为数字时,但是当使用变量时,它不起作用。

我要做的是按列排序html表的元素。

我有例如4列:

$colname = array("Column1", "Column2", "Column3", "Column4");

我从URL变量中获取已经排序的元素的ID:

$strTaskIds = $_GET["taskIds"];
// for example: $strTaskIds = "3;1;32_4;5_6;36_34;7"

现在我将字符串拆分为2D数组并更新MySQL表:

$arrTaskIds = explode("_", $strTaskIds);

for($i = 0; $i < count($arrTaskIds); $i++) {
    $arrIdsPerCol = explode(";", $arrTaskIds[$i]);

    for($j = 0; $j < count($arrIdsPerCol); $j++) {
        $sql = "UPDATE tasks SET col='$colname[$i]', rank=$j WHERE id=$arrIdsPerCol[$j]";
    }

    if($conW->query($sql) === TRUE) {
        $error = 0;
    } else {
        $error = 1;
    }
}

当我写一个数字E.G 7而不是变量$arrIdsPerCol[$j]时,它可以正常工作。

(int)$arrIdsPerCol[$j]也不起作用。

1 个答案:

答案 0 :(得分:0)

我给你没有错误信息的原因是没有。它看起来就像MySQL表没有更新 在我的代码主演很长一段时间后,发现了问题 我将query()代码放在外部循环中。但我在内循环中需要它。 问题解决了:

$arrTaskIds = explode("_", $strTaskIds);
$error = 0;

for($i = 0; $i < count($arrTaskIds); $i++) {
  $arrIdsPerCol = explode(";", $arrTaskIds[$i]);

  for($j = 0; $j < count($arrIdsPerCol); $j++) {
    $sql = "UPDATE tasks SET col='$colname[$i]', rank=$j WHERE id=$arrIdsPerCol[$j]";
    if($conW->query($sql) === TRUE) {
    } else {
      $error = 1;
    }
  }
}