I have tried creating this delete fuction where a delete button will appear on each row f the table created. when it came to testing the delete button when it was clicked it looks like it was doing something but nothing happened it looked like it just reloaded the page and nothing else and it never deleted the chosen user id. I really think I am close to solving this problem.

here my code which I done:

<?php 

if ($_POST['action'] == 'delete') 
{ 
   $sql = "DELETE FROM azizpassword WHERE user_id = {$_POST['user_id']}"; 


	$conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
	mysql_select_db("aziz",$conn) or die("Could Not Select The Database"); 



$result=mysql_query("SELECT user_id,az_firstname,az_surname,username FROM azizpassword ORDER BY user_id;");

$i=0;
while( $row=mysql_fetch_array($result) )
{
	if($i>0)
	{
		echo "<tr valign=top>";
		echo "<td bgcolor=#93B0B4 colspan=4></td>";
		echo "</tr>";
	}
	echo "<tr valign=center>";
	echo "<td width=100 class=table><b>".$row['user_id']."</b></td>";
	echo "<td width=100 class=table><a href=\"view.php?id=".$row['user_id']."\">".$row['az_surname']."</a></td>";
	echo "<td width=100 class=table>".$row['az_firstname']." </td>";
	echo "<td width=100 class=table><i>".$row['username']."</i> </td>";
	echo '<td width="100" class="table">'. 
   ' <form action="" method="post">'. 
   '  <input type="hidden" name="action" value="delete" />'. 
   '  <input type="hidden" name="user_id" value="'.$row['user_id'].'" />'. 
   '  <input type="submit" value="Delete" />'. 
   ' </form>'. 
   '</td>'; 

	echo "<td class=table></td>";
	echo "</tr>";
	$i++;
}
	}

echo "<tr valign=bottom>";
    echo "<td bgcolor=#93B0B4 colspan=6 height=8></td>";
    echo "</tr>";





?>

I think we are really close to solving the problem but I just dunno what I may of done wrong I hope anyone can find the problem with my code and spot something I may have missed to allow me to delete the the chosen user.
Also I have had trouble with creating a warning messagewhich asks the user if they are sure the want to delete the user I would be really greatful if anyone can help me out with this problem too

Thank you for your time

Take Care

    Did you post the complete code? If so, you've got some serious problems here:

    <?php  
    
    if ($_POST['action'] == 'delete')  
    {
    $sql = "DELETE FROM azizpassword WHERE user_id = {$_POST['user_id']}"; $conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); mysql_select_db("aziz",$conn) or die("Could Not Select The Database");

    You define the correct query in $sql to delete the user, but... you're not even connected to the database yet! Once you connect, you don't even run the $sql query; you go on to select the users again!

    You need to reverse the order and execute the $sql statement like so:

    <?php  
    
    if ($_POST['action'] == 'delete')  
    {
    $conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database");
    mysql_select_db("aziz",$conn) or die("Could Not Select The Database"); $sql = "DELETE FROM azizpassword WHERE user_id = {$_POST['user_id']}";
    mysql_query($sql) or die('Error running query: ' . mysql_error());

    If that solves your problem, you might also want to search the board for solutions to 'SQL Injections'. I've made several posts, all including the same code (using [man]get_magic_quotes_gpc/man to determine if magic_quotes_gpc is On or not, and then using [man]addslashes/man or [man]mysql_real_escape_string/man appropriately).

      thanks a lot mate, cheers for looking into this, im going to try your way, 🙂 thanks for being really helpful!

        Write a Reply...