Jump to content

delete a selected row from the database


bryanptcs

Recommended Posts

Ok, I have a pretty simple table to view my DB items through the website...what I want to be able to do is push the submit button and have it delete the row that coincides with the button....is this possible?  Below is my code...I have left the action for the submit button form as XXX b/c I don't know what to put there.  Thanks for your help.

 

<?php
  include("db.inc");
  
  $connection = mysql_connect($host, $user, $password) or die ("Could not connect");
  $db = mysql_select_db($database, $connection) or die ("Could not connect to DB");
  
  $sql = "SELECT * FROM ClientList ORDER BY companyName";
  $result = mysql_query($sql) or die("Could not execute query");
  
  echo "<table cellpadding='0' border='1' cellspacing='0' width='500' align='center'>";
  echo "<tr><td></td><td>Company Name</td>";
  echo "<td>Company Web Site</td></tr>";
  while ($row = mysql_fetch_array($result))
  	{
		extract($row);
		echo "<tr><td><form id='form1' name='form1' method='post' action='XXXX'><input type='submit' name='Submit' value='Submit' /></form><td>";
		echo $companyName."</td>";
		echo "<td><a href='http://".$companyWeb."' target='_blank'>".$companyWeb."</a></td>";
		echo "</tr>";
	}
	echo "</table>";
?>

Link to comment
https://forums.phpfreaks.com/topic/40617-delete-a-selected-row-from-the-database/
Share on other sites

Okay, first of all you need to figure out the primary key of your clientlist table...I will just call it clientID for right now, you can change it to suit you.

 

<?php
  include("db.inc");
  
  $connection = mysql_connect($host, $user, $password) or die ("Could not connect");
  $db = mysql_select_db($database, $connection) or die ("Could not connect to DB");
  
   if ($_POST['delete']){
    $deleteID = $_POST['delete'];

    mysql_query("DELETE from ClientList WHERE clientID = '$deleteID'")or die(mysql_error());

    echo "Successfully deleted client<p>";
  
    }


  $sql = "SELECT * FROM ClientList ORDER BY companyName";
  $result = mysql_query($sql) or die("Could not execute query");
  
  echo "<table cellpadding='0' border='1' cellspacing='0' width='500' align='center'>";
  echo "<tr><td></td><td>Company Name</td>";
  echo "<td>Company Web Site</td></tr>";
  while ($row = mysql_fetch_array($result))
  	{
		extract($row);

echo "<tr><td><form id='form1' name='form1' method='post' action='{$_SERVER['PHP_SELF']}'>
<input type='submit' name='delete' value='{$row['clientID']}' />
</form><td>";
		echo $companyName."</td>";
		echo "<td><a href='http://".$companyWeb."' target='_blank'>".$companyWeb."</a></td>";
		echo "</tr>";
	}
	echo "</table>";
?>

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.