使用针对 auto_incremented int 的 php 删除 MySQL 中的记录?
为什么这个删除不能删除整个记录:
$query = 'DELETE FROM tblEvents WHERE index = $_GET["id"]';
$result = mysql_query($query, $db) or die(mysql_error($db));
Where index is variable of type int, auto_incremented in MySQL?
Why doesnt this delete work to delete the whole record:
$query = 'DELETE FROM tblEvents WHERE index = $_GET["id"]';
$result = mysql_query($query, $db) or die(mysql_error($db));
Where index is variable of type int, auto_incremented in MySQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题与 php 相关,而不是 mysql。
print $query;
并查看。然后参考 php 字符串语法, http://php.net/types.string 了解正确的语法。
此外,进入查询的变量必须正确准备、转义,或者,如果是整数值,则手动转换为这种类型,
或者,为了使其成为单行,
索引也是保留字,这也可能导致问题,你可以用反引号转义它,
但是如果你将它重命名为
id
会更好Your question php is related, not mysql.
print $query;
and see.then refer to php strings syntax, http://php.net/types.string for the proper syntax.
Also, a variable that goes to the query, must be properly prepared, escaped, or, in case of integer value, manually cast to this type,
or, to make it single line,
also, index is reserved word that can cause problems too, you can escape it with backticks,
but it will be much better if you rename it to just
id
您应该使用单独的查询来测试删除是否成功
You should test for delete success with a separate query
Shrapnel 上校是对的,不能直接在单引号中的字符串中使用变量。如果您在查询周围使用双引号,它将起作用。
编辑:正如 Shrapnel 上校在他的评论中所说,在这种情况下,您还必须将数组偏移量中的双引号更改为单引号。
Col. Shrapnel is right, you can't use variables directly in a string in single quotes. If you use double quotes around your query, it will work.
EDIT: As Col. Shrapnel said in his comment, in this case you'll also have to change the double quotes in the array offset to single quotes.
希望您已经知道这一点,但您需要保护
$_GET['id']
以便人们无法进行 SQL 注入。尝试使用以下内容:这也解决了在单引号而不是双引号中使用变量的问题。
如果你愿意,你也可以这样做:
这也有效。
Hopefully you already know this, but you need to secure that
$_GET['id']
so people can't do SQL Injection. Try using the following instead:This also solves your problem of using a variable in single quotes instead of double quotes.
if you wanted you could also do:
This works too.