Jump to content

Need help deleting records using PHP


mrjtfool

Recommended Posts

Hi eveyone. I'm trying to write a PHP script that will delete an entry from my database using a basic HTML drop down list to select which entry should be deleted.

 

Here is the code to create the drop down box screen:

<?php
//Connect to the database.
$mysqli = mysqli_connect("localhost", "myusername", "mypassword", "mydatabase");
//Check if the database has any records.
$sql = 'SELECT *'
        . ' FROM salesperson'
        . ' WHERE salesperson.branch_no =001 ';
$check_branch_res = mysqli_query($mysqli, $sql)
				or die(mysqli_error($mysqli));

if (mysqli_num_rows($check_branch_res) < 1) 
{
$content = "
<p>There are no entries at this branch</p>";
} 

else //Create the form...
{ 
$content .= "<form action='delete.php' method='POST'>
			 <select name='number_select'>";
//Use while loop to go through database entries and output them to the user.
while ($branch_info = mysqli_fetch_array($check_branch_res))
{
$salesman_number = $branch_info['salesman_no'];
$salesman_fname = $branch_info['first_name'];
$salesman_lname = $branch_info['last_name'];

//Use the fields held in the database to create the list of items in the drop down box. 
$content .= "
<option value='$salesman_number'>$salesman_number $salesman_fname $salesman_lname</option>";
}   //Save the selected option as the variable $salesman_number 
    //(this depends on what option the user selects from the drop down menu).
$content .= "</select>
		 <br></br>
		 <input type='submit' value='Delete'/>
		 </form>";
}
?>	
<html>
<head>
<title>Delete a salesman</title>
<link rel=StyleSheet href="main.css" type="text/css" media=all>
</head>
<body>
<div id="header"><h1>Antrim</h1><p>Delete Selection</p></div>
<ul class="nav">
<li><a href="home.html" class="home">Home</a></li>
<li><a href="Antrim.php" class="view">View Equipment List</a></li>
<li><a href="Antrim_add.html" class="add">Add Entry</a></li>
<li><a href="home.html" class="modify">Modify Entry</a></li>
<li><a href="delete_Antrim.php" class="erase">Delete Entry</a></li>
</ul>
<?php echo $content;?>
</body>
</html>

 

The user will select a record to delete and press "Delete" below the drop down box which then sends this selection to my php script which should use the information from the form to delete various entries from various in my database.

 

Here is the code:

<?php
//Connect to the database
$mysqli = mysqli_connect("localhost", "myusername", "mypassword", "mydatabase");
//Delete a record from the first table based on the data from the form.
$sql = "DELETE FROM salesperson WHERE salesman_no = '".$_POST["salesman_number"]."'";
$res = mysqli_query($mysqli, $sql)
				or die(mysqli_error($mysqli));
//Delete a record from the second table based on the data from the form.
$sql = "DELETE FROM hhc WHERE salesman_no = '".$_POST["salesman_number"]."'";
$res = mysqli_query($mysqli, $sql)
				or die(mysqli_error($mysqli));
//Delete a record from the third table based on the data from the form.
$sql = "DELETE FROM printer WHERE salesman_no = '".$_POST["salesman_number"]."'";
$res = mysqli_query($mysqli, $sql)
				or die(mysqli_error($mysqli));
//Delete a record from the fourth table based on the data from the form.
$sql = "DELETE FROM single_dock WHERE salesman_no = '".$_POST["salesman_number"]."'";
$res = mysqli_query($mysqli, $sql)
				or die(mysqli_error($mysqli));
if ($res === TRUE) {
	echo "<p>The salesman has been deleted.</p>
	<b></b>
	<a href='home.html'>Go Back</a>";
} else {
	printf("Could not delete salesman: %s\n", mysqli_error($mysqli));
};
mysqli_close($mysqli);
?>

 

When you select a record to delete the script displays the message "The salesman has been deleted" and I can go back to my home screen.

 

However, the record still appears in the database.

 

Does anyone have any idea how I can make this work?

 

Thanks!!

Link to comment
https://forums.phpfreaks.com/topic/147334-need-help-deleting-records-using-php/
Share on other sites

I use check boxs beside each entry but im sure it will be the same idea so hears the code i use..

 

The form

<form method="post" action="profile_blog_delete.php?username=<? echo"".$_SESSION["myusername"]."" ?>">
<?php

include("config_members.php");

// Retrieve data from database 
$sql="SELECT * FROM User_blogT WHERE Blog_username='".$_SESSION["myusername"].  "' ORDER BY Blog_id DESC" ;
$result=mysql_query($sql);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<table class="myprofile_blog">
<tr><td class="myprofile_subtext">Subject :</td><td class="myprofile_blogsub"><? echo $rows['Blog_subject']; ?></td></tr>
<tr><td class="myprofile_subtext" style="width: 120px" valign="top">Message :</td><td class="myprofile_blogtext"><? echo $rows['Blog_message']; ?></td></tr>
<tr><td class="myprofile_subtext">Written on :</td><td class="myprofile_blogsub"><? echo $rows['Blog_date']; ?></td></tr>
<tr><td class="myprofile_subtext">Delete entry :</td><td><input type="checkbox" name="checkbox[]" id="<?=$rows['Blog_id']?>" value="<?=$rows['Blog_id']?>" /></td></tr>
</table>
<br />
<br />
<?
// close while loop 
}

// close connection 
mysql_close();
?>
<br />
<table>
<tr><td class="myprofile_changesh">Delete an entry from your Blog ...</td><td class="myprofile_changesh">
	 </td></tr>
<tr><td class="myprofile_subsub">Make sure you tick the box of the entry&#39;s 
	you want to delete before pressing the delete button otherwise it will 
	not work.</td></tr>
<tr><td colspan="2"></td></tr>
</table>
<br />
<table class="myprofile_change">
<tr><td><input name="perform" id="perform" type="submit" value="Delete that shit !" /></td></tr>
</table>
</form>

 

the php

<?php
include("config_blog.php");
$username=$_GET['username'];
if(!isset($_POST['perform'])){

} else {

foreach($_POST['checkbox'] as $Blog_id) { // This will loop through the checked checkboxes
$sql = "DELETE FROM User_blogT WHERE Blog_id='$Blog_id' LIMIT 1";
mysql_query($sql) or die(mysql_error());
header("location:profile_blog.php?username=$username");	

}
}
mysql_close();
?>

 

hopefully this will help you get your head around what to do as im not to good with drop down menus

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.