PHP编辑/删除mysql中的多行

时间:2013-01-07 17:10:40

标签: php mysql

晚上好,我正在尝试创建一个可以在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 个答案:

答案 0 :(得分:3)

猜猜你需要del_id而不是id

$sql = "UPDATE events SET title = '$title', description ='$description' WHERE id = $del_id ";  

答案 1 :(得分:0)

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

将其更改为value= '$id'

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

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

4-以前关于变量$del_id的答案。

5- u缺少结束标记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>

答案 2 :(得分:0)

for循环中的

id = $ del_id,仔细检查绑定到它们的SQL语句和变量。

答案 3 :(得分:-1)

放弃了上面的代码,并从

开始使用此示例
http://bohemiawebsites.com/PHP-MYSQL-Update-Multiple-Rows.html

如果有人需要,一切都有效......