简体   繁体   English

如何通过php从mysql中删除一行

[英]How to delete a row from mysql via php

Brushing up on php and working on a simple program where I've run into an issue. 刷新php并处理一个我遇到问题的简单程序。 I can't seem to figure out how to delete a mysql row. 我似乎无法弄清楚如何删除mysql行。 I will link my script in a pastie document so you can see how I have it set up. 我将在一个pastie文档中链接我的脚本,以便您可以看到我如何设置它。

I'm not familiar with AJAX or Javascript.. so I just made the delete button a form. 我不熟悉AJAX或Javascript ..所以我只是将删除按钮作为一个表单。 I'd like to keep it like this for now if I can make it work. 如果我能使它工作,我现在想保持这样。

PASTIE HERE 在这里PASTIE

change: 更改:

mysql_query("DELETE FROM name WHERE name=.'$del'.");

to: 至:

mysql_query("DELETE FROM name WHERE name='".$_POST['$del']."'");

becuase: 监守:
1. you should get rid of the . 你应该摆脱它。 inside the query, dot is used for string concatenation. 在查询内部,dot用于字符串连接。
2. you want to use the value of $_POST['$del'] - the parameter $del is not set 2.你想使用$_POST['$del'] - 未设置参数$del

Updates: 更新:

  1. change <input type="hidden" name="del" /> to: <input type="hidden" name="del" value="theNameYouWantToDelete"/> <input type="hidden" name="del" />更改为: <input type="hidden" name="del" value="theNameYouWantToDelete"/>
  2. you give the same name to all the form elements (name="del") - this is not recommended! 您为所有表单元素(name =“del”)指定相同名称 - 不建议这样做! better set a different name to each object. 更好地为每个对象设置不同的名称。
  3. please do not use mysql_* - it's deprecated and vulnerable to sql-injection, use PDO or MySQLi instead. 请不要使用mysql_* - 它已被弃用且易受sql注入攻击,请改用PDO或MySQLi。

You're not properly concatenating the $del variable. 你没有正确连接$del变量。 You could simply use: 你可以简单地使用:

mysql_query("DELETE FROM name WHERE name='$del'");

Also, you need to set $del before the DELETE query. 此外,您需要在DELETE查询之前设置$del That variable hasn't been declared before you tried to use it, so it will be null . 在您尝试使用它之前尚未声明该变量,因此它将为null

On line 30 you have two inputs named del , and the second one does not contain the name to delete 在第30行,您有两个名为del输入,第二个不包含要删除的名称

echo '<form id="del" method="post"><input type="submit" name="del" value="X" /><input type="hidden" name="del" /></form>';

Need to change to something like - 需要换成类似的东西 -

echo '<form id="del" method="post"><input type="submit" name="del_submit" value="X" /><input type="hidden" name="del" value="'.$del.'" /></form>';

Then change lines 12-13 to 然后将第12-13行更改为

if (isset($_POST['del_submit'])) {
mysql_query("DELETE FROM name WHERE name='".$_POST['del']."'");

Please note that mysql_ functions are depreciated, and you are subject to sql injection. 请注意, mysql_函数已被折旧,并且您需要使用sql注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM