Jump to content

Deleting an entry from mysql without deleting all other entries echod


Recommended Posts

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

 

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?

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....

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.

 

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>";

						}

 

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>";
  }
}

 

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>";
  }
}

 

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!?

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>

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>

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>

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.