gitn32 Posted May 25, 2013 Share Posted May 25, 2013 Here is my code: $result = mysql_query ("SELECT * FROM $tbl_name WHERE username='$user'"); while($row = mysql_fetch_array($result)) { $username = $row['username']; $email = $row['password']; $age = $row['age']; $id = $row['id']; echo " <font size='3' face='arial'> <table width='250' border='0' cellspacing='1' cellpadding='0'> <tr> <td><b>$id, $username,$password,$email,$age</b></td> </tr> <tr> <td><form action=delete.php method=POST><input type=submit name=submit value=Remove></form></td> </tr> </table> </font> "; if(isset($_POST['submit'])) { $got_error = 0; mysql_query(" DELETE $id FROM $tbl_name); } else { $got_error = 1; echo "Error."; } } I have multiple pieces of data. Each piece of data has its own submit buttton when displayed. If there is 2 pieces of data and I hit the top button, the bottom data is deleted. I want to make it so that if I hit the top button the top button is deleted. Please help. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 25, 2013 Share Posted May 25, 2013 You need to submit the items unique id in the form. Try using a hidden input. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 25, 2013 Share Posted May 25, 2013 And then delete from table WHERE id = your id Quote Link to comment Share on other sites More sharing options...
gitn32 Posted May 25, 2013 Author Share Posted May 25, 2013 Thank you! If you have time can you show me what the edited one is. Once I add the hidden input I would like to check that it is correct. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 25, 2013 Share Posted May 25, 2013 What? Quote Link to comment Share on other sites More sharing options...
gitn32 Posted May 25, 2013 Author Share Posted May 25, 2013 Re-Write my code with hidden input. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 25, 2013 Share Posted May 25, 2013 AHAHAHAAAAA!! Quote Link to comment Share on other sites More sharing options...
gitn32 Posted May 25, 2013 Author Share Posted May 25, 2013 ...? Quote Link to comment Share on other sites More sharing options...
tqla Posted May 25, 2013 Share Posted May 25, 2013 Each time the form appears you need to include a hidden field containing that records id. Something like this: <input type="hidden" name="id" value="<?php echo $id ?>"> Then change your query to: DELETE FROM $tbl_name WHERE id = $_POST['id']; Of course, format it in a way where you won't get errors. My formatting isn't like yours but you get the idea. Quote Link to comment Share on other sites More sharing options...
gitn32 Posted May 25, 2013 Author Share Posted May 25, 2013 Thank you. Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 25, 2013 Share Posted May 25, 2013 remember to sanitize your data $id=mysql_real_escape_string($_POST['id']; DELETE FROM ... WHERE id = $id; to protect against sql injection attacks Quote Link to comment Share on other sites More sharing options...
gitn32 Posted May 25, 2013 Author Share Posted May 25, 2013 Thank you. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 25, 2013 Share Posted May 25, 2013 Why would you bother escaping an integer? Just make it an int. Quote Link to comment Share on other sites More sharing options...
buzzycoder Posted May 25, 2013 Share Posted May 25, 2013 You can use jquery. Quote Link to comment Share on other sites More sharing options...
gitn32 Posted May 25, 2013 Author Share Posted May 25, 2013 (edited) Ok the first problem is solved, how about this: $result = mysql_query ("SELECT * FROM invoices WHERE username='$user'"); while($row = mysql_fetch_array($result)) { $username = $row['username']; $bill = $row['invoice']; $ammount = $row['ammount']; $id = $row['id']; echo " <font size='3' face='arial'> <table width='250' border='0' cellspacing='1' cellpadding='0'> <tr> <td><b>$id, $bill: $$ammount</b></td> </tr> <tr> <td><form action=bills.php method=POST<input type=submit name=submit value=Remove</form></td> </tr> </table> </font> "; if(isset($_POST['submit'])) { $got_error = 0; if ($accbal > $ammount){ $naccbal = $accbal - $ammount; require ("userconnect.php"); mysql_query("UPDATE users SET accbal = '$naccbal' WHERE username = '$user'"); } else { $got_error = 1; echo "Not enough money in account."; } } //$accbal IS defined before code starts //And there is an extra $ before $ammount so it displays a dollar sign, and the ammount. I'm also having the same problem with this, and this is different because your updating not deleting. This is a virtual funding system. Edited May 25, 2013 by gitn32 Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 26, 2013 Share Posted May 26, 2013 Why would you bother escaping an integer? Just make it an int. How do I know that id is an integer? Probably is but I don't think that you can tell from the code. If it is an integer and is never going to change to a string then you are right making it an int will be faster. Quote Link to comment Share on other sites More sharing options...
gitn32 Posted May 26, 2013 Author Share Posted May 26, 2013 Ok the first problem is solved, how about this: $result = mysql_query ("SELECT * FROM invoices WHERE username='$user'"); while($row = mysql_fetch_array($result)) { $username = $row['username']; $bill = $row['invoice']; $ammount = $row['ammount']; $id = $row['id']; echo " <font size='3' face='arial'> <table width='250' border='0' cellspacing='1' cellpadding='0'> <tr> <td><b>$id, $bill: $$ammount</b></td> </tr> <tr> <td><form action=bills.php method=POST<input type=submit name=submit value=Remove</form></td> </tr> </table> </font> "; if(isset($_POST['submit'])) { $got_error = 0; if ($accbal > $ammount){ $naccbal = $accbal - $ammount; require ("userconnect.php"); mysql_query("UPDATE users SET accbal = '$naccbal' WHERE username = '$user'"); } else { $got_error = 1; echo "Not enough money in account."; } } //$accbal IS defined before code starts //And there is an extra $ before $ammount so it displays a dollar sign, and the ammount. I'm also having the same problem with this, and this is different because your updating not deleting. This is a virtual funding system. Can someone please reply. Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 26, 2013 Share Posted May 26, 2013 This is wrong <form action=bills.php method=POST< No quotes around bills.php or POST, no > to close form tag. Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 26, 2013 Share Posted May 26, 2013 Same issues here <input type=submit name=submit value=Remove< You should code in an IDE that will color code things and highlight mistakes. I use netbeans which is free and can be found at netbeans.org . Quote Link to comment Share on other sites More sharing options...
gitn32 Posted May 28, 2013 Author Share Posted May 28, 2013 Thanks, but that wasn't the question...... Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 29, 2013 Share Posted May 29, 2013 I believed that I answered the question: told you what the errors were and quoted the two lines of code that had the errors in them. I then went on to suggest that if you were coding in a good IDE all of the errors you posted would have been far less likely to occur and easier to spot. Please read my posts again. Quote Link to comment Share on other sites More sharing options...
gitn32 Posted May 30, 2013 Author Share Posted May 30, 2013 Yes that was helpful. But not to this question: I have multiple pieces of data. Each piece of data has its own submit button when displayed. If there is 2 pieces of data and I hit the top button, the bottom data is the accbalance. I want to make it so that if I hit the top button the top button the newaccbal is the amount of the top. Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 30, 2013 Share Posted May 30, 2013 My guess is that you need a </form><form> between the two submit buttons so that only the top or bottom information gets submitted, but I'd need to see the form and the script to do more than guess. Quote Link to comment Share on other sites More sharing options...
gitn32 Posted June 1, 2013 Author Share Posted June 1, 2013 Ok the first problem is solved, how about this: $result = mysql_query ("SELECT * FROM invoices WHERE username='$user'"); while($row = mysql_fetch_array($result)) { $username = $row['username']; $bill = $row['invoice']; $ammount = $row['ammount']; $id = $row['id']; echo " <font size='3' face='arial'> <table width='250' border='0' cellspacing='1' cellpadding='0'> <tr> <td><b>$id, $bill: $$ammount</b></td> </tr> <tr> <td><form action=bills.php method=POST<input type=submit name=submit value=Remove</form></td> </tr> </table> </font> "; if(isset($_POST['submit'])) { $got_error = 0; if ($accbal > $ammount){ $naccbal = $accbal - $ammount; require ("userconnect.php"); mysql_query("UPDATE users SET accbal = '$naccbal' WHERE username = '$user'"); } else { $got_error = 1; echo "Not enough money in account."; } } //$accbal IS defined before code starts //And there is an extra $ before $ammount so it displays a dollar sign, and the ammount. I'm also having the same problem with this, and this is different because your updating not deleting. This is a virtual funding system. There it is. Quote Link to comment Share on other sites More sharing options...
boompa Posted June 1, 2013 Share Posted June 1, 2013 <form action=bills.php method=POST<input type=submit name=submit value=Remove</form> Valid HTML would be helpful here. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.