0

到目前为止,我所拥有的是(我认为是)一个工作循环,迭代并获得正确的 id(由 var_dump 确认),但我的 sql 查询没有将 id 作为 DELETE 键。本质上,SQL 查询是删除 id = 数组迭代的当前 id 值的地方。使用多个复选框。var dump 正在确认 id 与上传 id 匹配,但是我无法进行任何删除。这是代码:

       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!"; echo 'wtf'; }
  //return; }//bail if its empty
  require_once ('../mysqli_connect.php'); //Connect to the db

  $delete_success = false; var_dump($delete_success);
  foreach ($id_array as $id) { var_dump($id);
    $remove = "DELETE FROM upload WHERE upload_id= $id";//AND `owner_id`=".$_SESSION['user_id'];
    $result = mysqli_query ($dbc, $remove); // Run the query
    //
    ////$mysqli->query($sql) or die(mysqli_error($mysqli));
    if ($result) { $delete_success = true; var_dump($delete_success);}
    mysqli_close($dbc);
  }



  if($delete_success == true) { echo 'done';
    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);
4

1 回答 1

1

您需要从函数中删除该mysqli_close($dbc);语句。deleteUploads()我也同意你应该努力接受人们的回答。您已经提出了 9 个问题,但您没有接受 13 个答案之一。这真的不公平。

编辑 我已经快速浏览了您的脚本并将其移动了一下。这是否提供任何有用的信息?

<?
$msgs[] = 'Log: Started';
require_once('../mysqli_connect.php');

function submit_delete()
{
    global $msgs;
    if(!is_null($_POST['delete']) && !is_null($_POST['checkbox']))
    {
        $msgs[] = "Log: submit_delete called with valid checkbox delete.";
        $id_array = $_POST['checkbox'];
        deleteUploads($id_array);
    }else{
        $msgs[] = "Error: submit_delete called without valid checkbox delete.";
    }
}

function deleteUploads ($id_array)
{
    global $msgs;
    if (count($id_array) <= 0)
    {
        $msgs[] = "Error: No deletes in id_array!";
    }else{
        $msgs[] = "Log: Deletes in id_array!";
    }

    $delete_success = false;
    foreach ($id_array as $id)
    {
        $msgs[] = "Log: Processing id: ".$id;
        $remove = "DELETE FROM upload WHERE upload_id = $id";
        $result = mysqli_query ($dbc, $remove);
        if ($result)
        {
            $msgs[] = 'Log: delete success = true';
            header('Location: newwriter_profile.php');
        }else{
            $msgs[] = 'Error: '.mysqli_error();
        }
    }
}

submit_delete();

if(!@mysqli_close($dbc))
{
    $msgs[] = 'Error: mysqli_close failed';
}
echo implode('<br>',$msgs);
?>
于 2012-07-13T06:15:39.143 回答