0

Brushing up on php and working on a simple program where I've run into an issue. I can't seem to figure out how to delete a mysql row. I will link my script in a pastie document so you can see how I have it set up.

I'm not familiar with AJAX or Javascript.. so I just made the delete button a form. I'd like to keep it like this for now if I can make it work.

PASTIE HERE

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Brandon
  • 125
  • 1
  • 11

3 Answers3

2

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.
2. you want to use the value of $_POST['$del'] - the parameter $del is not set

Updates:

  1. change <input type="hidden" name="del" /> to: <input type="hidden" name="del" value="theNameYouWantToDelete"/>
  2. you give the same name to all the form elements (name="del") - this is not recommended! 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.
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

You're not properly concatenating the $del variable. You could simply use:

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

Also, you need to set $del before the DELETE query. That variable hasn't been declared before you tried to use it, so it will be null.

Blake
  • 1,691
  • 2
  • 15
  • 23
  • Blake, could you instruct me how to get this working then? I declared $del in the while loop but I guess I can't access it outside of that loop. – Brandon Aug 14 '12 at 04:29
0

On line 30 you have two inputs named del, and the second one does not contain the name to delete

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

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.

Sean
  • 12,443
  • 3
  • 29
  • 47
  • Sean, this worked simply and perfectly. Thank you for your input. – Brandon Aug 14 '12 at 04:38
  • Sean, could you provide me with a link on switching my mysql functions to PDO or something more secure? I wasn't aware they were depreciated. – Brandon Aug 14 '12 at 04:48
  • You can look at [http://www.php.net/manual/en/mysqlinfo.api.choosing.php](http://www.php.net/manual/en/mysqlinfo.api.choosing.php) – Sean Aug 14 '12 at 05:00
  • Sean, here is my revised code. I've converted it to mysqli. Could you tell me if this is more secure, and other ways to improve it? I'd really appreciate it! http://pastie.org/4471333 – Brandon Aug 14 '12 at 05:15
  • Yes, it is more secure. Noticed that when you changed to mysqli you removed `$_POST['del']` from line 13, so now `$del` in `$con->query("DELETE FROM name WHERE name='$del'");` is not set. Make sure to add `$del = $_POST['del']` before line 13. Also make sure this page is only accessible to those who you want to be able to delete names. – Sean Aug 14 '12 at 05:36
  • Well I should of been clearer. I guess other ways you could advise to improve security.. would it be necessary to use "mysql_real_escape_string" since I'm using mysqli now? Those types of things. And the script works perfectly as it is now.. should I add `$del = $_POST['del']` back in even though it's working perfectly? – Brandon Aug 14 '12 at 05:41
  • If it is working, no need to add it back in. I am also learning `mysql_*` to `mysqli_*`, so I cannot provide the best help on security/sanitizing. Here are 2 links to look at - [http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) and [http://stackoverflow.com/questions/3327974/when-to-sanitize-php-mysql-code-before-being-stored-in-the-database-or-when-it](http://stackoverflow.com/questions/3327974/when-to-sanitize-php-mysql-code-before-being-stored-in-the-database-or-when-it) – Sean Aug 14 '12 at 05:50