4

I'm beginner in PHP MySQL, all I want to know how to create a DELETE function for this each specific records.

enter image description here

Here is my code when I SELECT the records. I don't know how I will insert the DELETE function and how code structure be like.

$res=mysql_query("SELECT * FROM accounts WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);


$sql = "SELECT * FROM accounts ORDER BY agentFname ASC";

 if($result = mysql_query($sql)){
     if (mysql_num_rows($result) > 0 ) {



             <table id="example" class="table table-striped responsive-utilities jambo_table">

                 <thead>

                      <tr class="headings">

              <?php           

                   echo "<th>ID</th>"; // Primary Key
                   echo "<th>Agent Code</th>"; // Unique Key
                   echo "<th>First Name</th>";  
                   echo "<th>Middle Name</th>";    
                   echo "<th>Last Name</th>";     
                   echo "<th>Contact Number</th>";     
                   echo "<th>Address</th>";     
                   echo "<th>Gender</th>";
                   echo "<th>User Type</th>"; 
                   echo "<th>Action</th>";        
                   echo "</tr>"; 
                   echo "</thead>"; 

           }  while($row = mysql_fetch_array($result))  {

               echo"<tbody>";
               echo " <tr class=\"even pointer\">"; 

               echo "<td>" .$row['user_id']. "</td>";  // Primary Key
               echo "<td>" .$row['agentCode']. "</td>";  // Unique Key
               echo "<td>" .$row['agentFname']. "</td>";
               echo "<td>" .$row['agentMname']. "</td>";
               echo "<td>" .$row['agentLname']. "</td>";
               echo "<td>" .$row['agentContact']. "</td>";
               echo "<td>" .$row['agentAddress']. "</td>";
               echo "<td>" .$row['agentGender']. "</td>";
               echo "<td>" .$row['user_type']. "</td>";
               echo "<td> <a href=\"#.php\"> Delete</a></td>"; // DELETE Function
               echo "</tr>";
                }

            }
1
  • I hope those are not real phone numbers... Commented Oct 9, 2015 at 4:43

2 Answers 2

2

Looks like you are tryin to do something like

echo "<td><a href="/yourfile.php?delete=<?php echo $row['user_id'];?>"> Delete</a></td>

then in yourfile.php you would handle the deletion and redirect back to the original page but...

This would delete the record even if you click there by mistake.

You could make little forms there with a checkbox each or have some js asking to confirm deletion first...

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. but it's too close.
What I'm looking is the delete.php codes. By the way will try your suggestion using checkbox. Thanks!
1

just pass the user id in href with file name.

echo "<td> <a href="yourfile.php?id=<?php echo $row['user_id'];?>"> Delete</a></td>";

then create your file. example your filename.php

<?php

$id=$_GET['id'];

$sql=mysql_query("DELETE FROM tablename where user_id='$id'");

if($sql > 0)
{
  echo "record successfully deleted";
}
else
{
 echo "errore in deleting";
}

?>

2 Comments

You're great! Thanks!
@mak mysql_query is deprecated , you shouldn't encourage the people to use, use mysqli or pdo instead of mysql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.