为什么我的PHP删除按钮不起作用?

时间:2014-03-27 00:29:21

标签: php mysql button html-table sql-delete

我在这里做错了什么?我正在尝试创建一个表,每行旁边都有一个删除按钮/链接,单击时删除该行。

<?php

ini_set('display_errors',1); 
 error_reporting(E_ALL);

mysql_connect('localhost', 'root', 'ca229');

mysql_select_db('testdb');

$result = mysql_query('select * from products');

$numrows = mysql_numrows($result);

//****************************************************************
print "<table border = 3 style = width:400px>";


for($i = 0; $i < $numrows; $i++)
{
    $row = mysql_fetch_row($result);

    print "<tr>";


    foreach($row as $cell)
    {

        print "<td>";
        print $cell;
        print "</td>";



    }
      print "<td><a href='delete.php?id=".$row["id"] ."' >Delete</a> " ;


    print "</tr>";


}


print "</table>";
//***************************************************************   

mysql_close();

?>

和...

<?php

$id = $_GET["id"];
$delete = " DELETE * from products where id = ". $id ;

mysql_query($delete) ; 

?>

2 个答案:

答案 0 :(得分:1)

在delete.php文件的开头添加数据库连接行。

mysql_connect('localhost', 'root', 'ca229');
mysql_select_db('testdb');

您需要打开mysql_query的连接才能使用您的数据库。

答案 1 :(得分:1)

在我们继续讨论好的东西之前,让我先指出原始错误的位置。

由于$row = mysql_fetch_row($result);创建了一个枚举数组(例如:array(0 =&gt;'value1',1 =&gt;'value2',...)),而不是关联数组,$row["id"]将是未定义的。这仍然会生成HTML,但所有删除按钮都没有指定id。

这一行:

$delete = "DELETE * from products where id = ". $id;

您不需要*只需执行:

$delete = "DELETE from products where id = ". $id;

现在,好东西)---顺便说一下,我使用id - product - city作为列示例。

这是mysqli_*版本。将xxx替换为您的数据库凭据。

<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);

DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');
DEFINE ('DB_NAME', 'xxx');

$db = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");

$results = mysqli_query($db,"select * from products");

print "<table border=1 cellspacing=\"0\" cellpadding=\"3\">\n";

echo "<tr>\n";
echo '<td width="25%">
      <p align="center">ID</td>
    <td width="25%">
      <p align="center">PRODUCT</td>
    <td width="25%">
      <p align="center">CITY</td>
    <td width="25%">
      <p align="center">ACTION</td>
  </tr>';

while ($row = mysqli_fetch_assoc($results)) {

    print "<tr><td>" . $row['id'] . "</td>\n<td>" . $row['product'] . "</td>\n" . "<td>" . $row['city'] . "</td>\n" . "<td>\n"  . "<a href='delete.php?id=".$row["id"] ."'>Delete</a>\n</td>\n</tr>\n";

}

print "</table>\n";

echo "<hr>"; // simply a seperator. You can delete this

mysqli_close($db);

?>

删除代码 - mysqli_*(delete.php)

<?php
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');
DEFINE ('DB_NAME', 'xxx');

$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");

$id = (int)$_GET["id"];

$delete = "DELETE from products where id = ". $id;
// You can also use
// $delete = "DELETE from products where id=$id";

if (mysqli_query($db,$delete))
  {
  echo "Database updated successfully";
  }
else
  {
  echo "An error occurred: " . mysqli_error($db);
  }

?>

mysql_*函数弃用通知:

http://www.php.net/manual/en/intro.mysql.php

从PHP 5.5.0开始,不推荐使用此扩展,不建议用于编写新代码,因为将来会删除它。相反,应使用mysqliPDO_MySQL扩展名。在选择MySQL API时,另请参阅MySQL API Overview以获得进一步的帮助。

这些功能允许您访问MySQL数据库服务器。有关MySQL的更多信息,请访问»http://www.mysql.com/

可以在»http://dev.mysql.com/doc/找到MySQL的文档。

以下是一些有关预备语句的教程,您可以学习并尝试:

以下是PDO的一些教程:

相关问题