我想构建一个删除页面,用于从我的数据库中删除一些数据。
当我把id放入其中工作正常,但我希望它从网址中提取id
www.example.com/delete.php?id=1234
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'DELETE FROM MYTABLE
WHERE created=<?php echo $_GET["id"]; ?>';
mysql_select_db('ely');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
?>
有问题的部分是这部分
WHERE created=<?php echo $_GET["id"]; ?>';
我得到的错误是“无法删除数据:您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以便在第2行'='附近使用正确的语法”
答案 0 :(得分:0)
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$id = mysql_real_escape_string($_GET['id']);
$sql = "DELETE FROM MYTABLE
WHERE created='$id'";
mysql_select_db('ely');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
?>
答案 1 :(得分:0)
学习引用!
$sql = 'DELETE FROM MYTABLE
WHERE created=<?php echo $_GET["id"]; ?>';
$sql = 'Delete from MYTABLE WHERE created="'.$_GET['id'].'"';
接下来就是google for sql-injections,请记住你应该证明是否及时设置了$ _GET ['id']
答案 2 :(得分:0)
答案 3 :(得分:0)
试试这个:
$id = $_GET['id'];
$sql = 'DELETE FROM MYTABLE
WHERE created=' .$id;
“”。用于连接变量$ id
的值至于连接数据库我建议您查找PDO。
答案 4 :(得分:0)
$sql="DELETE FROM MYTABLE WHERE created='".$_GET['id']."'";