Foreach mysqli删除循环麻烦

时间:2012-05-16 03:41:07

标签: php checkbox mysqli foreach

我非常接近解决我的问题,这就是我所在的地方:

           while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC))
    {
        echo '<tr><td align="left">' .
        $row['title'] . '</td><td align="left">'
        . $row['genre'] . '</td><td align="left">'
        . $row['length'] . '</td><td align="left">'
        . $row['created'] . '</td><td align="left">'
        . $row['views'] . '</td><td align="left">' //. var_dump($row) //dump row value for testing
        . '<input type="checkbox" name="checkbox[]"  value= "'.$row['upload_id'].'"'.' />'.' </td>'. '</tr>';



    }
    echo '</table>'; // Close the table

和删除功能:

function submit_delete() {
  if(!is_null($_POST['delete']) && !is_null($_POST['checkbox'])) { //Check to see if a delete command has been submitted.
    //This will dump your checkbox array to your screen if the submit works.
    //You can manually check to see if you're getting the correct array with values
    var_dump($_POST['checkbox']);//Comment this out once it's working.
    deleteUploads($id_array);

  }
  else {
    echo "Error: submit_delete called without valid checkbox delete.";var_dump($_POST['checkbox']);
  }
}


    function deleteUploads ($id_array) {
      if (count($id_array) <= 0) {   echo "Error: No deletes in id_array!"; var_dump($_POST['checkbox']); return; }//bail if its empty
      $delete_success = false;
      foreach ($id_array as &$id) {
        $sql = "DELETE FROM `upload` WHERE `upload_id`=$id AND `owner_id`={$_SESSION['user_id']}";
        $result = $mysqli->query($sql) or die(mysqli_error($mysqli));
        if ($result) { $delete_success = true; }
      }

      if($delete_success) {
        header('Location: newwriter_profile.php');
      } else {
        echo "Error: ".mysqli_error();
      }
    }


    //Test deleteUploads (remove once you know the function is working)
    //$test_ids = array();
    //$test_ids[] = 5;//make sure this id is in the db
    //$test_ids[] = 7;//this one too
    submit_delete();
    //deleteUploads($id_array);//should remove ids 10 and 9//

    mysqli_close($dbc);

我已确定id_array没有为该复选框获取正确的$_POST值。 vardump显示它具有正确的上传ID,然后我vardump id_array并显示NULL。任何帮助将不胜感激。

目前:

        function submit_delete() {
  if(!is_null($_POST['delete']) && !is_null($_POST['checkbox'])) { //Check to see if a delete command has been submitted.
    //This will dump your checkbox array to your screen if the submit works.
    //You can manually check to see if you're getting the correct array with values
   // var_dump($_POST['checkbox']);//Comment this out once it's working.
    $id_array = $_POST['checkbox'];
    var_dump($id_array);
    deleteUploads($id_array);

  }
  else {
    echo "Error: submit_delete called without valid checkbox delete.";//var_dump($_POST['checkbox']);
  }
}


function deleteUploads ($id_array) {
  if (count($id_array) <= 0) {   echo "Error: No deletes in id_array!"; return; }//bail if its empty
  $delete_success = false;
  foreach ($id_array as $id) {
    $sql = "DELETE FROM `upload` WHERE `upload_id`=$id AND `owner_id`={$_SESSION['user_id']}";
    $result = $mysqli->query($sql) or die(mysqli_error($mysqli));
    if ($result) { $delete_success = true; }
  }

  if($delete_success) {
    header('Location: newwriter_profile.php');
  } else {
    echo "Error: ".mysqli_error();
  }
}

我在id_array = POST之后得到了正确的vardump,但是在我提交删除之后,viewsource表示在我读完之后没有任何内容(侧边栏和页脚消失)。目前没有报告任何错误。

1 个答案:

答案 0 :(得分:1)

似乎没有为$ id_array分配值的任何地方。例如,您致电:

deleteUploads($id_array)

此时没有为$ id_array分配值。

$ id_array应该有什么价值?您需要将$ _POST数组中的值分配给$ id_array吗?

此外,我认为可能存在错位的'&amp;'这里:

foreach ($id_array as &$id)

我认为应该只是:

foreach ($id_array as $id)