PHP删除表单,用于从MySQL数据库中删除数据

时间:2014-05-27 16:45:50

标签: php mysql forms sql-delete

我正在制作一个用于从MySQL表中删除数据的PHP脚本。 但它没有用,有人能告诉我我做错了什么吗? 下面你可以看到我的两个php脚本,用于删除数据库中的信息。

第一个脚本     

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// select record from mysql 
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
var_dump($sql)
?>

<table>
<tr>
<td><strong>Hardware verwijderen</strong> </td>
</tr>

<tr>
<td><strong>HardwareID</strong></td>
<td><strong>Merk</strong></td>
<td><strong>Producttype</strong></td>
<td>&nbsp;</td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td><? echo $rows['hardwareID']; ?></td>
<td><? echo $rows['merknaam']; ?></td>
<td><? echo $rows['producttype']; ?></td>
<td><a href="delete_hardware.php?id=<? echo $rows['id']; ?>">delete</a></td>
</tr>

<?php
// close while loop 
}
?>

</table>

<?php
// close connection; 
mysql_close();
?>

第二个脚本

    <?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="helpdesk_middenpolder"; // Database name 
$tbl_name="hardware"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar 
$id=$_GET['id'];

// Delete data in mysql from row that has this id 
$sql="DELETE FROM $tbl_name WHERE hardwareID='$id'";
$result=mysql_query($sql);

// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='testinlogpagina.php'>Terug naar de startpagina</a>";
}

else {
echo $sql . '. Details: ' . mysql_error();
}
?> 

<?php
// close connection 
mysql_close();
?>

**编辑

var_dump:string'SELECT * FROM hardware'(length = 22)

的结果

2 个答案:

答案 0 :(得分:0)

因为您正在发送像这样的GET数据

<a href="delete_hardware.php?id=<? echo $rows['hardwareID']; ?

此处您使用id作为属性/键

所以得到这个值如下

$id=$_GET['id'];

答案 1 :(得分:0)

在delete_hardware.php上

更改行

// get value of id that sent from address bar 
$id=$_GET['hardwareID'];

// Delete data in mysql from row that has this id 
$sql="DELETE FROM $tbl_name WHERE id='$id'";

// get value of id that sent from address bar 
$id=$_GET['id'];

// Delete data in mysql from row that has this id 
$sql="DELETE FROM $tbl_name WHERE hardwareID='$id'";