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

6
  • please post the specific code you're having problems with Commented Aug 14, 2012 at 4:19
  • The script is only 45 lines of code. You want me to paste the whole thing here instead of my pastie link? Commented Aug 14, 2012 at 4:22
  • your pastie link is just fine Commented Aug 14, 2012 at 4:22
  • You asked how to delete a row from mysql. You could include just the one or two relevant lines, then the question because useful for everyone, even if pastie isn't available. Commented Aug 14, 2012 at 4:23
  • Make sure you are cleaning your post data for SQL injection! Or use binding with PDO. Commented Aug 14, 2012 at 4:24

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

7 Comments

He would also need to pass relevant data with $_POST['del']. Which he isn't.
@Brandon - please try again :)
No parse error this time. But the row still exists in the table unfortunately.
That's because $_POST['del'] has no data... like I said.
@Brandon - please echo the value of $_POST['del'] before the query runs
|
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.

7 Comments

Sean, this worked simply and perfectly. Thank you for your input.
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.
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! pastie.org/4471333
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.
|
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.