0

我有一个表格可以从我的 sql 数据库中删除一行。我昨天让它正常工作,但由于某种原因,它无法成功删除任何内容,只是返回一条确认消息。我知道我所有的登录/连接详细信息都是正确的。请告诉我这个脚本是否有错误。

PHP

if(isset($_POST['delete']))
{
$dbhost = '';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$address = $_POST['address'];

$sql = "DELETE FROM units WHERE address = '$address'";

mysql_select_db('db_units');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
}
else
{

形式

<form method="post" action="<?php $_PHP_SELF ?>">

<td><input name="address" type="text" address="address"></td>

<input name="delete" type="submit" address="delete" value="Delete">
4

1 回答 1

2

All functions in PHP prefixed mysql_ are deprecated. Using the functions prefixed with mysqli_ or the OOP version is recommended. Here is code that I'm sure would work, I don't have the privilege of testing right now because I'm on my phone. I personally use the object-oriented version of mysqli, but this is mostly up to you.

<?php

$link = new mysqli( $host, $user, $password, $database );

if ( isset( $_POST['delete'] ) ) {

    $address = $_POST['address'];

    $sql = "DELETE FROM `units` WHERE `address` = '{$address}'";

    if ( ! $retval = $link->query( $sql ) )
        die( "Could not delete data: " . $link->error );

}

?>

This is obviously just internal code and for simplicity I didn't sanitize/validate anything. However, proper sanitation & validation is a must. You can learn more about mysqli here

于 2013-11-14T22:00:36.013 回答