Web Hosting Talk







View Full Version : Delete Row from MySQL in PHP


bigkid
08-05-2004, 02:06 AM
Hi everyone.

Im trying to make a system that allows me to add, edit, delete and display customer details in html.

Currently I have 2 scripts, one to view and the other to add. These pages are called customers.php and add_customers.php. I am wanting 2 more scripts where I can delete the row of information aswell as edit the row.

These files are wanted to be called edit_customers.php and delete_customers.php.

I have tried modifing a script from Spoono's tutorials which allows me to edit the row but I have had little success adding that into my already made website.

Does anyone here know how to make a search system where the user can search for the customer name and it say the record, then have the option to edit or delete it? If it edits, then show a list of the current information in a form where I can edit it?

Hopefully someone out there can help me as I have been looking on the net for the past few days and cant find anything...

Thanks everyone!

Burhan
08-05-2004, 02:37 AM
The reason you can't find anything is because this is something very simple and people are coding these kinds of scripts everday.

I'll give you some skeleton code which you can customize:

I'm assuming you have a database setup with a table called customers that has three fields, id, name, and email address.

File: search.php


<?php
error_reporting(E_ALL);
/* Insert database connection code here */
if ($_SERVER['REQUEST_METHOD'] != "POST")
{
//We need to display the form
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Please type the name of the customer (leave blank for all customers):
<input type="text" name="cust_name" />
<input type="submit" name="submit" value="Search" />
</form>
<?php
}
if (@$_POST['cust_name'] == "")
{
$query = "SELECT * FROM `customers`";
} else {
$query = "SELECT * FROM `customers` WHERE `name` = '".mysql_escape_string($_POST['cust_name'])."'";
}
$result = mysql_query($query);
if (!$result) { die($query."<br />".mysql_error()); }
if (mysql_num_rows($result) != 0)
{
//We have some records
while($row = mysql_fetch_assoc($result))
{
echo "Customer Name : ".$row['name']."<br />";
echo "Customer Email : ".$row['email']."<br />";
echo "Action : <a href=\"edit.php?id=".$row['id']."\">edit</a> | <a href=\"del.php?id=".$row['id']."\">delete</a><br /><br />";
}
} else {
echo "Sorry, there were no results for your search";
}
?>


File: edit.php


<?php
error_reporting(E_ALL);
/* Database connection code */
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
//We have some changes to make to the record
$name = mysql_escape_string($_POST['name']);
$email = mysql_escape_string($_POST['email']);
$id = mysql_escape_string($_POST['id']);
$query = "UPDATE `customers` SET `name` = '".$name."', ";
$query .= "`email` = '".$email."' WHERE `id` = '".$id."'";
if (!mysql_query($query)) { die($query."<br />".mysql_error()); }
echo "Record Updated";
} else {
if (@$_GET['id'] == "")
{
die ("Sorry, you cannot access this script directly");
}
if (!is_numeric(@$_GET['id']))
{
die ("Invalid Id");
}
$id = $_GET['id'];
//Grab the record
$query = "SELECT * FROM `customer` WHERE id = '".mysql_escape_string($id)."'";
$result = mysql_query($query);
if (!$result) { die($query."<br />".mysql_error()); }
$row = mysql_fetch_assoc($result);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="hidden" name="id" value="<?php echo $row['id'] ?>" />
Customer Name : <input type="text" name="name" value="<?php echo $row['name']; ?>" /><br />
Customer Email : <input type="text" name="email" value="<?php echo $row['email']; ?>" /><br />
<input type="submit" name="submit" value="Edit Record" />
</form>
<? } ?>


File : del.php


<?php
error_reporting(E_ALL);
/* Database connection code */
if (@$_GET['id'] == "")
{
die ("Sorry, you cannot access this script directly");
}
if (!is_numeric(@$_GET['id']))
{
die ("Invalid Id");
}
$id = mysql_escape_string($_GET['id']);
$query = "DELETE FROM `customers` WHERE `id` = '".$id."' LIMIT 1";
if (!mysql_query($query)) { die($query."<br />".mysql_error()); }
echo "Record deleted successfully";
?>


Of course, you would need to modify them and add your own error checking/customizations, but it should get you started.

bigkid
08-05-2004, 07:25 AM
Thanks heaps fyrestrtr!

Your a life saver. Ill try it out and tell you how it goes.

bigkid
08-06-2004, 04:32 AM
Well it took me a bit but I got them to work :)