Jump to content

Delete mysql row using html button


guru62

Recommended Posts

hello im trying to delete a user from users table by using a button. This is my code:

$sql= "SELECT 
			user_name

	FROM users";


$result = mysql_query($sql);

if(!$result)
{
echo 'Users could not be displayed, please try again later.' . mysql_error();
}
else
{
if(mysql_num_rows($result) == 0)
{
	echo 'There are no users.';
}
else
{

echo '<table border="1">
				  <tr>
					<th>Users</th>
					<th>Delete user and their posts?</th>

				  </tr>';
 while ($row = mysql_fetch_array($result)) 
       {
       if($_SERVER['REQUEST_METHOD'] != 'POST')
{
       
         echo '<form method="post" action=""><tr>';
					echo '<td class="leftpart">';

					echo $row['user_name'];
					echo '</td>';
					echo '<td class="rightpart"><form method="post" action=""><input type="submit" name="delete" value="DELETE - '. $row['user_name'] .' "></form></td>';


				echo '</tr>';
  }
  
  else
        {
        
        
        $sql="DELETE FROM users

		WHERE user_name = " . mysql_real_escape_string(($_POST['delete']);

        
        
        }
  
  }
  }
  
        
        }

 

 

When I click the button, it does not delete the record, any ideas?

Link to comment
https://forums.phpfreaks.com/topic/259879-delete-mysql-row-using-html-button/
Share on other sites

no luck, i want to use button to select the user that is displayed and delete their record but im having trouble in running the mysql query, here is my updated code, the delete query is at the bottom:

 

$sql= "SELECT 
			user_name

	FROM users";


$result = mysql_query($sql);

if(!$result)
{
echo 'Users could not be displayed, please try again later.' . mysql_error();
}
else
{
if(mysql_num_rows($result) == 0)
{
	echo 'There are no users.';
}
else
{

echo '<table border="1">
				  <tr>
					<th>Users</th>
					<th>Delete user and their posts?</th>

				  </tr>';
 while ($row = mysql_fetch_array($result)) 
       {
       if($_SERVER['REQUEST_METHOD'] != 'POST')
{
       
         echo '<form method="post" action=""><tr>';
					echo '<td class="leftpart">';

					echo $row['user_name'];
					echo '</td>';
					echo '<td class="rightpart"><form method="post" action=""><input type="submit" name="delete" value="DELETE - '. $row['user_name'] .' "></form></td>';


				echo '</tr>';
  }
  
  else
        {
        
        
        $sql="DELETE FROM users

		WHERE user_name = " . mysql_real_escape_string($_GET['user_name']);

        
        
        }
  
  }
  }
  
        
        }

i just looked at the code more and there are so many problems i'm not sure where to start.. couple other big things that stick out to me..

 

there is a check on request method for each row in the table? why?

 

a button loads the page, so there is no point in putting the delete in the loop.

Archived

This topic is now archived and is closed to further replies.

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