1

晚上好,我正在尝试制作一个可以在 mysql 中编辑/删除多行的 php 页面:

<html>  
<head>  
<title>Update/Delete Test Page</title>  
</head>  
<body> 

<?  
include 'connect.php';

if(isset($_POST['edit'])) // from button name="delete"
{
    $title = $_POST['title'];
    $description = $_POST['description'];
    if(!empty($_POST['checkbox'])) {
    foreach($_POST['checkbox'] as $check) {
        $ed = $check;
        $sql = "UPDATE events SET title = '$title', description ='$description' WHERE id = $ed";  
    }
    }

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con);
}
    ?>

<?php
include ("connect.php");

if(isset($_POST['delete'])) // from button name="delete"
{
    if(!empty($_POST['checkbox'])) {
    foreach($_POST['checkbox'] as $check) {
        $del = $check;

        $sql = "DELETE from events where id = $del";
        $result = $mysqli->query($sql) or die(mysqli_error($mysqli));
}
}
?>  

<?php
include 'connect.php';
$query = 'SELECT id, title, description FROM events WHERE evdate = "1/9/2013" order by title asc';
$result2 = $mysqli->query($query) or die(mysqli_error($mysqli));

echo '<br><br><br>';
echo '<b><div align="center"> "1/9/2013"</div></b>';

if ($result2) {

  // create a new form and then put the results
  // into a table.
  echo "<form method='post' action=".$_SERVER['PHP_SELF'].">"; 
  echo "<table cellspacing='0' cellpadding='3'>
    <th align='left'>Interval orar</th>
    <th align='left'>Eveniment</th>
    <th align='left'></th>
    ";

  while ($row = $result2->fetch_object()) {

    $title  = $row->title;
    $description = $row->description;
    $id     = $row->id;

    //put each record into a new table row with a checkbox
echo "
    <tr>
        <td align='left'><input type='text' name='title' size='20'  value='$title'></td>
        <td align='left'><input type='text' name='description' size='50'  value='$description'></td>
        <td align='left'><input type='checkbox' name='checkbox[]' id='checkbox[]'  value=$id />
     </tr>

     "; 
}

// when the loop is complete, close off the list.
echo "</table>
<p>
<input id='edit' type='submit' class='button' name='edit' value='Edit'/> 
<input id='delete' type='submit' class='button' name='delete' value='Delete'/>
</p>
</form>";
}   
?>
</body>  
</html>

Connect.php 看起来像这样:

<?php
$db_host = "localhost";
$db_username = "calendar";
$db_pass = "calendar";
$db_name = "ecalendar";
$con = mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("could not connect to     mysql database");
mysql_select_db("$db_name") or die ("no database");

$mysqli = new MySQLi($db_host, $db_username, $db_pass, $db_name) or die(mysqli_error());
?>

删除工作正常,但编辑没有做任何事情......

表如下所示:

Start/End Time         Event     (Checkbox is here)
08:00-10:00            test1               X
10:00-12:00            test2               X

当我尝试将 test2 编辑为 test2xx 时,我在 Firebug POST 中得到了这个:

title=08%3A00-10%3A00&description=test1&title=10%3A00-12%3A00&description=test2xx&checkbox%5B%5D=53&edit=Edit

如果我删除我会得到这个(它有效)

title=08%3A00-10%3A00&description=test1&title=10%3A00-12%3A00&description=test2&checkbox%5B%5D=53&delete=Delete

编辑工作但仅适用于最后一行(test2),如果我尝试编辑上面的行(test1),它会使用最后一行(test2)的值更新它

4

4 回答 4

3

猜你del_id不需要id

$sql = "UPDATE events SET title = '$title', description ='$description' WHERE id = $del_id ";  
于 2013-01-07T17:12:55.793 回答
0

1-value=$id在你的 chekbob 中看你的价值

将其更改为value= '$id'

2-而且,删除查询是用mysqli的,但更新查询是用mysql的??

3-你不必包含connect.php 3次

4-还有关于变量的先前答案$del_id

5-你缺少结束标签td

6-你错过了关闭}

7-试试这个

<html>  
 <head>  
 <title>Update/Delete Test Page</title>  
 </head>  
 <body> 

 <?php  
 include 'connect.php';

 if(isset($_POST['edit'])) // from button name="edit"
 {
 $checkbox = $_POST['checkbox']; //from name="checkbox[]"
 $countCheck = count($_POST['checkbox']);

 for($i=0;$i<$countCheck;$i++)
 {
    $del_id  = $checkbox[$i];

  $sql3 = "UPDATE events SET title = '$title', description ='$description' WHERE id = '$del_id' ";  
  $result3 = $mysqli->query($sql3) or die(mysqli_error($mysqli));
 if (!$result3)
 {
 die(mysqli_error($mysqli)) ;
 }
 echo "1 record added";

 }
 }



  if(isset($_POST['delete'])) // from button name="delete"
 {
 $checkbox = $_POST['checkbox']; //from name="checkbox[]"
 $countCheck = count($_POST['checkbox']);

 for($i=0;$i<$countCheck;$i++)
 {
    $del_id  = $checkbox[$i];

    $sql = "DELETE from events where id = '$del_id' ";
    $result = $mysqli->query($sql) or die(mysqli_error($mysqli));

  }
  }


  $query = 'SELECT id, title, description FROM events WHERE evdate = "1/9/2013" order by title asc';
  $result2 = $mysqli->query($query) or die(mysqli_error($mysqli));

  echo '<br><br><br>';
  echo '<b><div align="center"> "1/9/2013"</div></b>';

 if ($result2) {

 // create a new form and then put the results
 // into a table.
 echo "<form method='post' action=".$_SERVER['PHP_SELF'].">"; 
 echo "<table cellspacing='0' cellpadding='3'>
 <th align='left'>Interval orar</th>
 <th align='left'>Eveniment</th>
 <th align='left'></th>
 ";

 while ($row = $result2->fetch_object()) {

 $title  = $row->title;
 $description = $row->description;
 $id     = $row->id;

  //put each record into a new table row with a checkbox
 echo "
 <tr>
    <td align='left'><input type='text' name='title' size='20'  value='$title'></td>
    <td align='left'><input type='text' name='description' size='50'    value='$description'></td>
    <td align='left'><input type='checkbox' name='checkbox[]' id='checkbox[]'  value='$id' /></td>
  </tr>

 "; 
 }

 // when the loop is complete, close off the list.
 echo "</table>
 <p>
 <input id='edit' type='submit' class='button' name='edit' value='Edit'/> 
  <input id='delete' type='submit' class='button' name='delete' value='Delete'/>
  </p>
 </form>";
 }   
 ?>
 </body>  
 </html>
于 2013-01-07T17:40:41.010 回答
0

id=$del_id 在 for 循环下,仔细检查你的 SQL 语句和绑定到它们的变量。

于 2013-01-07T18:31:22.137 回答
-1

放弃了上面的代码并从这个例子开始

http://bohemiawebsites.com/PHP-MYSQL-Update-Multiple-Rows.html

如果有人需要,一切正常...

于 2013-01-09T11:29:58.923 回答