jdock1 Posted April 11, 2011 Share Posted April 11, 2011 I have a bad way with words, so if the title doesnt make much sense nor my post, I apolagize! But I am using a while loop and mysql_fetch_array to echo all information in a table in mysql. With each entry, I have included a button that executes a delete query for that entry. $delquery="DELETE FROM requests WHERE user = '".$row['user']."'"; mysql_query($delquery,$link2); echo "<font size='19px' color='#009933'>User deleted from request database. When I click the button, the message is echod in every entry listed on the page, and nothing happens. I figured Id use delete from the username, so that way mysql will know which one to delete... but Idk when I think about it thats dumb and makes no sense. Im stuck. This side of the application im trying to make is the admin side where I can process requests. After I process the request I delete it from the database so I know I processed it. I have no idea what query to use for this. Anybody know how I could do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/ Share on other sites More sharing options...
spiderwell Posted April 11, 2011 Share Posted April 11, 2011 hard to tell without seeing more of the code, but essentially each record should have a unique identifer (primary key). use this to tie the echoed message to that specific id. Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1200321 Share on other sites More sharing options...
jdock1 Posted April 12, 2011 Author Share Posted April 12, 2011 hard to tell without seeing more of the code, but essentially each record should have a unique identifer (primary key). use this to tie the echoed message to that specific id. Your right, I quit coding for a few months and just started back up. Forgot all about that. I created id MEDIUMINT NOT NULL AUTO_INCREMENT but now I do not know how to insert an actual ID into the database. Its supposed to automatically insert and ID into the table during a query, but its not working. I do not know what to put the value as for that field? Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1200379 Share on other sites More sharing options...
jdock1 Posted April 12, 2011 Author Share Posted April 12, 2011 Disregard that last post, I used NULL and its assigning ids now. Now, this should work just using $delquery="DELETE FROM requests WHERE user = '".$row['id']."'"; that way, it will only delete the entry with that ID? IDK, let me post the code.... Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1200383 Share on other sites More sharing options...
jdock1 Posted April 12, 2011 Author Share Posted April 12, 2011 Heres the code, I know its really messy but thats what Im most comfortable with. The deal is, I need to delete that entry only when I click the button. Im using that query to delete by id. $query="select * from requests"; $delquery="DELETE FROM requests WHERE id = '".$row['id']."'"; $result=mysql_query($query,$link2); $num_rows = mysql_num_rows($result); echo "<font style='font-size:20px'>Current unproccessed cashout requests: $num_rows \n</font><br><br>"; while ($row = mysql_fetch_array($result)){ echo "<font style='font-size: 16px'><b><u>User ID:</font></u></b><br>"; echo $row['user']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Date:</font></b></u><br>"; echo $row['date']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>PayPal Email:</u></font></b><br>"; echo $row['paymail']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Comments:</u></font></b><br>"; echo $row['comments']; echo "<form action='' method='post'><br><br> <input type='submit' name='delete' value='User Paid' /> </form>"; if ($_POST['delete'] == "User Paid") { mysql_query($delquery,$link2); echo "<font size='19px' color='#009933'>User deleted from request database.</font><br>"; } echo "<img src='imgs/hr.png' alt='hr' />"; echo "<br>"; } That query doesnt work still, using the ID. It echos the message on each entry, and nothing is being deleted. How can I do this?? So stumped. Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1200384 Share on other sites More sharing options...
jdock1 Posted April 12, 2011 Author Share Posted April 12, 2011 Sorry, thought that was for php code. Heres the code in a better code view: $query="select * from requests"; $delquery="DELETE FROM requests WHERE id = '".$row['id']."'"; $result=mysql_query($query,$link2); $num_rows = mysql_num_rows($result); echo "<font style='font-size:20px'>Current unproccessed cashout requests: $num_rows \n</font><br><br>"; while ($row = mysql_fetch_array($result)){ echo "<font style='font-size: 16px'><b><u>User ID:</font></u></b><br>"; echo $row['user']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Date:</font></b></u><br>"; echo $row['date']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>PayPal Email:</u></font></b><br>"; echo $row['paymail']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Comments:</u></font></b><br>"; echo $row['comments']; echo "<form action='' method='post'><br><br> <input type='submit' name='delete' value='User Paid' /> </form>"; if ($_POST['delete'] == "User Paid") { mysql_query($delquery,$link2); echo "<font size='19px' color='#009933'>User deleted from request database.</font><br>"; } echo "<img src='imgs/hr.png' alt='hr' />"; echo "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1200386 Share on other sites More sharing options...
dcro2 Posted April 12, 2011 Share Posted April 12, 2011 Uhhh, well, you'd have to have a special section of your code or another page if you like that specifically handles deleting a user. Why don't you change your submit button to this: <input type='submit' name='delete' value='".$row['id']."' /> That way you'll have the id that they want deleted in $_POST['delete'] and you can just do this, away from the section of code you posted, nearer the top: if(!empty($_POST['delete'])) { $delquery = "DELETE FROM requests WHERE id='".$_POST['delete']."'"; if(mysql_query($delquery, $link2)) { echo "<font size='19px' color='#009933'>User deleted from request database.</font><br>"; } else { echo "<font size='19px' color='#009933'>Failed to delete user from request database.</font><br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1200455 Share on other sites More sharing options...
jdock1 Posted April 12, 2011 Author Share Posted April 12, 2011 Uhhh, well, you'd have to have a special section of your code or another page if you like that specifically handles deleting a user. Why don't you change your submit button to this: <input type='submit' name='delete' value='".$row['id']."' /> Nice thinking! Thanks. That way you'll have the id that they want deleted in $_POST['delete'] and you can just do this, away from the section of code you posted, nearer the top: if(!empty($_POST['delete'])) { $delquery = "DELETE FROM requests WHERE id='".$_POST['delete']."'"; if(mysql_query($delquery, $link2)) { echo "<font size='19px' color='#009933'>User deleted from request database.</font><br>"; } else { echo "<font size='19px' color='#009933'>Failed to delete user from request database.</font><br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1200699 Share on other sites More sharing options...
jdock1 Posted April 12, 2011 Author Share Posted April 12, 2011 Actually, I just tried it, same thing happened. Echod the success message on every entry, and didint delete nothing. So I changed the query to instead of delete from requests where id =$_POST['delete'] to delete from requests where id = $row['id']... same thing, echod success message in every entry on the page, so i refreshed the page and it deleted everthing in the database. Ughhhhhhhhh now what!? Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1200709 Share on other sites More sharing options...
dcro2 Posted April 12, 2011 Share Posted April 12, 2011 Why don't you post all the code on that page so I can incorporate it in a better place? Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1200754 Share on other sites More sharing options...
jdock1 Posted April 16, 2011 Author Share Posted April 16, 2011 Heres the complete code; <div class="content"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <b></b></font> <br/> <font style="font-size: 18px"> <br></br> <b>Cashout Requests</b><br/> </font> <font style="font-size: 16px"> <br> <br> </br></br> </font> <form action="" method="post" name="winners"> <?php $hostname2="localhost"; $username2="**********"; $password2="**********"; $databasename2="**********"; $link2 = mysql_connect($hostname2, $username2, $password2); mysql_select_db("$databasename2"); $query="select * from requests"; $result=mysql_query($query,$link2); $num_rows = mysql_num_rows($result); echo "<font style='font-size:20px'>Current unproccessed cashout requests: $num_rows \n</font><br><br>"; while ($row = mysql_fetch_array($result)){ echo "<font style='font-size: 16px'><b><u>User ID:</font></u></b><br>"; echo $row['user']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Date:</font></b></u><br>"; echo $row['date']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>PayPal Email:</u></font></b><br>"; echo $row['paymail']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Comments:</u></font></b><br>"; echo $row['comments']; echo "<form action='' method='post'><br><br> <input type='submit' name='delete' value='User ID ".$row['id']." Paid' </form><br><br>"; echo "<img src='imgs/hr.png' alt='hr' />"; echo "<br>"; if(!empty($_POST['delete'])) { $delquery = "DELETE FROM requests WHERE id='".$row['id']."'"; if(mysql_query($delquery, $link2)) { echo "<font size='19px' color='#009933'>User deleted from request database.</font><br>"; } else { echo "<font size='19px' color='#009933'>Failed to delete user from request database.</font><br>"; } } } ?> </form> </table> <br /> <br /><br /><br /><br /><br /><br /><br /><br /><br /> </font> <div class="clear"></div> </div> Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1202238 Share on other sites More sharing options...
AtomicRax Posted April 16, 2011 Share Posted April 16, 2011 hmm, have you tried $delquery="DELETE FROM requests WHERE user = '".$row['user']."' LIMIT 1"; WAIT! If the $_POST['delete'] variable is set.. if(!empty($_POST['delete'])) { $delquery = "DELETE FROM requests WHERE id='".$row['id']."'"; it's going to do that for every row! that's why they're all getting deleted! Here's what I recommend: <div class="content"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <b></b></font> <br/> <font style="font-size: 18px"> <br></br> <b>Cashout Requests</b><br/> </font> <font style="font-size: 16px"> <br> <br> </br></br> </font> <form action="" method="post" name="winners"> <?php $hostname2="localhost"; $username2="**********"; $password2="**********"; $databasename2="**********"; $link2 = mysql_connect($hostname2, $username2, $password2); mysql_select_db("$databasename2"); // Only delete the deleteID user if(!empty($_POST['delete'])) { $delquery = "DELETE FROM requests WHERE id='".$_POST['deleteID']."'"; if(mysql_query($delquery, $link2)) { echo "<font size='19px' color='#009933'>User deleted from request database.</font><br>"; } else { echo "<font size='19px' color='#009933'>Failed to delete user from request database.</font><br>"; } } $query="select * from requests"; $result=mysql_query($query,$link2); $num_rows = mysql_num_rows($result); echo "<font style='font-size:20px'>Current unproccessed cashout requests: $num_rows \n</font><br><br>"; while ($row = mysql_fetch_array($result)){ echo "<font style='font-size: 16px'><b><u>User ID:</font></u></b><br>"; echo $row['user']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Date:</font></b></u><br>"; echo $row['date']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>PayPal Email:</u></font></b><br>"; echo $row['paymail']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Comments:</u></font></b><br>"; echo $row['comments']; echo "<form action='' method='post'><br><br> <input type='submit' name='deleteID' value='".$row['id']."'> <input type='submit' name='delete' value='User ID ".$row['id']." Paid'> </form><br><br>"; echo "<img src='imgs/hr.png' alt='hr' />"; echo "<br>"; } ?> </form> </table> <br /> <br /><br /><br /><br /><br /><br /><br /><br /><br /> </font> <div class="clear"></div> </div> Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1202245 Share on other sites More sharing options...
AtomicRax Posted April 16, 2011 Share Posted April 16, 2011 Sorry, my copy pasta job was horrible, here's the corrected version (I can't edit my post anymore ): <div class="content"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <b></b></font> <br/> <font style="font-size: 18px"> <br></br> <b>Cashout Requests</b><br/> </font> <font style="font-size: 16px"> <br> <br> </br></br> </font> <form action="" method="post" name="winners"> <?php $hostname2="localhost"; $username2="**********"; $password2="**********"; $databasename2="**********"; $link2 = mysql_connect($hostname2, $username2, $password2); mysql_select_db("$databasename2"); // Only delete the deleteID user if(!empty($_POST['delete'])) { $delquery = "DELETE FROM requests WHERE id='".$_POST['deleteID']."'"; if(mysql_query($delquery, $link2)) { echo "<font size='19px' color='#009933'>User deleted from request database.</font><br>"; } else { echo "<font size='19px' color='#009933'>Failed to delete user from request database.</font><br>"; } } $query="select * from requests"; $result=mysql_query($query,$link2); $num_rows = mysql_num_rows($result); echo "<font style='font-size:20px'>Current unproccessed cashout requests: $num_rows \n</font><br><br>"; while ($row = mysql_fetch_array($result)){ echo "<font style='font-size: 16px'><b><u>User ID:</font></u></b><br>"; echo $row['user']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Date:</font></b></u><br>"; echo $row['date']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>PayPal Email:</u></font></b><br>"; echo $row['paymail']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Comments:</u></font></b><br>"; echo $row['comments']; echo "<form action='' method='post'><br><br> <input type='hidden' name='deleteID' value='".$row['id']."'> <input type='submit' name='delete' value='User ID ".$row['id']." Paid'> </form><br><br>"; echo "<img src='imgs/hr.png' alt='hr' />"; echo "<br>"; } ?> </form> </table> <br /> <br /><br /><br /><br /><br /><br /><br /><br /><br /> </font> <div class="clear"></div> </div> Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1202247 Share on other sites More sharing options...
jdock1 Posted April 21, 2011 Author Share Posted April 21, 2011 Sorry, my copy pasta job was horrible, here's the corrected version (I can't edit my post anymore ): <div class="content"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <b></b></font> <br/> <font style="font-size: 18px"> <br></br> <b>Cashout Requests</b><br/> </font> <font style="font-size: 16px"> <br> <br> </br></br> </font> <form action="" method="post" name="winners"> <?php $hostname2="localhost"; $username2="**********"; $password2="**********"; $databasename2="**********"; $link2 = mysql_connect($hostname2, $username2, $password2); mysql_select_db("$databasename2"); // Only delete the deleteID user if(!empty($_POST['delete'])) { $delquery = "DELETE FROM requests WHERE id='".$_POST['deleteID']."'"; if(mysql_query($delquery, $link2)) { echo "<font size='19px' color='#009933'>User deleted from request database.</font><br>"; } else { echo "<font size='19px' color='#009933'>Failed to delete user from request database.</font><br>"; } } $query="select * from requests"; $result=mysql_query($query,$link2); $num_rows = mysql_num_rows($result); echo "<font style='font-size:20px'>Current unproccessed cashout requests: $num_rows \n</font><br><br>"; while ($row = mysql_fetch_array($result)){ echo "<font style='font-size: 16px'><b><u>User ID:</font></u></b><br>"; echo $row['user']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Date:</font></b></u><br>"; echo $row['date']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>PayPal Email:</u></font></b><br>"; echo $row['paymail']; echo "<br><br>"; echo "<font style='font-size: 16px'><b><u>Comments:</u></font></b><br>"; echo $row['comments']; echo "<form action='' method='post'><br><br> <input type='hidden' name='deleteID' value='".$row['id']."'> <input type='submit' name='delete' value='User ID ".$row['id']." Paid'> </form><br><br>"; echo "<img src='imgs/hr.png' alt='hr' />"; echo "<br>"; } ?> </form> </table> <br /> <br /><br /><br /><br /><br /><br /><br /><br /><br /> </font> <div class="clear"></div> </div> Oh sweet thanks bro! Sorry I havent got back to this post in a while I havent been able to get on a computer. Ima check it out ill post back when I get the results Quote Link to comment https://forums.phpfreaks.com/topic/233424-deleting-an-entry-from-mysql-without-deleting-all-other-entries-echod/#findComment-1204337 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.