【问题标题】:multiple checkbox delete mysqli多个复选框删除mysqli
【发布时间】:2012-05-21 01:40:00
【问题描述】:

我正在尝试获得多重删除功能,这就是我目前所拥有的:

<?php
  echo '<h3>Welcome to your profile, '. $_SESSION['first_name'] . '</h3>';

  require_once ('../mysqli_connect.php'); //Connect to the db

  // Make the query

  $q = "SELECT upload_id, title, genre, length, created, views
        FROM upload
        WHERE owner_id =". $_SESSION['user_id'] ."
        ORDER BY title ASC";


  $r = mysqli_query ($dbc, $q); // Run the query

  if($r) {
    ?>
    <table align="center" cellspacing="3" cellpadding="3" width="75%">
      <tr>
        <td align="left"><b>Title</b></td>
        <td align="left"><b>Genre</b></td>
        <td align="left"><b>Pages</b></td>
        <td align="left"><b>Submitted</b></td>
        <td align="left"><b>Views</b></td>';
          <form action="/nbproject/newwriter_profile.php" method="post">
          <?php
            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">'
                . '<input type="checkbox" name="checkbox[]"'
                . 'id="checkbox[]"  value='.$row['$upload_id'] //this upload id is auto-increment (see first note)
                .' />'.' </td>'. '</tr>';
            }
          ?>
    </table>
    <input type="submit" name="delete" value="Delete" align="right"/>
  </form>
  <?php
    mysqli_free_result ($r); // Free up the resources
  } else { // If it did not run okay
    // Public Message:
    echo '<p class="error">Your submissions could not be retrieved.  We apologize for any inconvenience.</p>';
    // Debugging message:
    echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
  } // End of if ($r) IF.

  mysqli_close($dbc); // Close the database connection

  if($_POST['delete']) {
    require_once ('../mysqli_connect.php'); //Connect to the db

    $checkbox = $_POST['checkbox']; //from name="checkbox[]"
    $countCheck = count($_POST['checkbox']);

    for($i=0;$i<$countCheck;$i++) {
      $del_id  = $checkbox[$i];
      //----------------------------------
      $sql = "DELETE from `upload` where `upload_id` = $del_id AND `owner_id` = {$_SESSION['user_id']}";
      $result = $mysqli->query($sql) or die(mysqli_error($mysqli)); //added session information after hearing about google spider armageddon scenarios
    }

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

第一个注意事项:基本上,注册用户会被他们的个人资料页面打招呼,该页面会显示他们的上传内容和一些上传信息。我想在每个上传旁边有一个复选框,在页面上的任何其他地方都有一个删除按钮。所以我尝试了这个。

现在它将重定向而没有错误,并且没有任何内容被删除。此外,在未选中复选框的情况下点击 Delete 会返回错误。提前感谢您的帮助,解决这个问题也将解决我遇到的另一个问题,这将是一个巨大的解脱。

我想不出将上传 ID 链接到复选框的方法。我的脑袋好痛。

编辑:这是复选框的视图源的当前输出,使用 dcoder 的推荐和 vardump 测试。 input type="checkbox" name="checkbox[]" id="checkbox[]" value="" />

vardump 测试输出:array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" }

编辑:删除功能目前如下:

    function submit_delete() {
    if($_POST['delete'] && $_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($_POST['checkbox[]']);
    }
   }


   function deleteUploads ($id_array) {
   if (count($id_array) <= 0) { 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);

编辑:目前如下:

        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[]"'. 'id="checkbox[]"  value=               "'.$row['upload_id'].'"'.' />'.' </td>'. '</tr>';

注释掉vardump,表明每个复选框都获得了正确的upload_id...

    function submit_delete() {
   if(!is_null($_POST['delete']) && !is_null($_POST['checkbox[]'])) { //Check to see if              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($_POST['checkbox[]']);

   }
   else {
   echo "Error: submit_delete called without valid checkbox delete.";
    }
   }


   function deleteUploads ($id_array) {
    if (count($id_array) <= 0) {   echo "Error: No deletes in id_array!";    var_dump($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();
 }
}


//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);

在页面加载时,我收到错误“submit_delete called...” 在我勾选一些框并点击删除后......我得到了同样的错误。退出 $_POST['checkbox[]'] 并获得 NULL 值。

【问题讨论】:

  • 对您的帖子进行了编辑以清理代码。请注意,您有很多无关的打开和关闭 php 标记,以及回显字符串与未处理标记的不稳定使用。你应该找到适合自己的风格并坚持下去。

标签: php checkbox mysqli


【解决方案1】:

我已将您的功能分解为您可以调用的功能。前一个函数将帮助您测试您是否获得了正确的值。您可以使用您知道数据库中的值手动填充数组以检查后一个函数。

function submit_delete() {
  if($_POST['delete'] && $_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($_POST['checkbox']);
  } else { 
    echo "Error: submit_delete called without valid checkbox delete.";
  }
}

function deleteUploads ($id_array) {
  if (count($id_array) <= 0) { //bail if its empty
    echo "Error: No deletes in id_array!";
    return; 
  }
  $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[] = 10;//make sure this id is in the db
$test_ids[] = 9;//this one too
deleteUploads($test_ids);//should remove ids 10 and 9

【讨论】:

  • 我将您的函数添加到我的代码中。它目前正在输出:array(3) { [0]=> string(1) "/" [1]=> string(1) "/" [2]=> string(1) "/" } 到我的屏幕关于复选框提交。我检查的值的数据库 id 值应分别为 5,6 和 7。我不是 100% 确定我是否正确读取了 vardump,但 string(1) 应该是 string(5), string(6), string(7) 吗?我正在使用 3 个数据库行作为测试,检查每个框,upload_id 的 5,6,7
  • 这确认它没有正确构造值。当页面打开时,复选框的实际 html 显示是什么? (查看源代码)。您可以将其添加到问题中吗?
  • 复选框的当前输出是:在视图中输入 type="checkbox" name="checkbox[]" id="checkbox[]" value="" />。这反映了下面 DCoder 建议的更改。
  • 我终于得到了正确的值发送到复选框,我如何改变你的功能来执行真正的删除?我注释掉了测试数组块以及 var_dump,但没有删除。提前致谢。
  • 您的输出中是否有字符串“Error:”?请务必查看 html 源代码,因为它可能不可见。
【解决方案2】:

问题出在这里:

'<input type="checkbox" name="checkbox[]"'
. 'id="checkbox[]"  value='.$row['$upload_id']
.' />'

将其更改为以下内容:

'<input type="checkbox" name="checkbox[]"'
. 'id="checkbox[]"  value="'.$row['upload_id'] . '"'
.' />'

您在值周围缺少引号("),并且您输出了错误的索引。


另外,请使用bind variables 将动态元素嵌入到SQL 查询中,并在将其输出到页面之前使用escape dynamic input

【讨论】:

  • 我按照您的建议更改了复选框,目前得到: input type="checkbox" name="checkbox[]" id="checkbox[]" value="" /> 作为视图源输出。
  • 那么你应该在打印之前var_dump($row) - 它看起来upload_id 列没有任何价值。
【解决方案3】:

我解决了这个问题。在用尽所有其他选项后,我在 MySQL 中运行了一个 grant * 权限语句。显然我在创建 Uploads 表之前创建了数据库用户。谢谢大家的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 2013-11-22
    相关资源
    最近更新 更多