biscuitboy
Technical User
Hiya,
Currently I am having problems with deleting a complete record from a mysql db. The code below shows that I am requesting the user to enter four fields that will identify the record in the database. In total there are four more fields in each record however I dont want the user to identify them. From this I would like it to locate the record and then delete the entire record.
However im unsure on how the code should be written.
Any help would be appreciated.
Best regards
code:
<?php # Scrtipt 6.6 - register.php
$page_title = 'Add New Entry';
if (isset($_POST['submit'])) { // Handle the form.
$message = NULL; // create an empty new variable.
//check for a surname
if (empty($_POST['surname'])) {
$ln = FALSE;
$message .= '<p>You forgot to enter your surname!</p>';
} else {
$ln = $_POST['surname'];
}
//check for a first name
if (empty($_POST['first_name'])) {
$fn = FALSE;
$message .= '<p>You forgot to enter your first name!</p>';
} else {
$fn = $_POST['first_name'];
}
//check for a flat no
if (empty($_POST['flat_no'])) {
$fln = FALSE;
$message .= '<p>You forgot to enter youe Flat No!</p>';
} else {
$fln = $_POST['flat_no'];
}
//check for a building name
if (empty($_POST['building_name'])) {
$bn = FALSE;
$message .= '<p>You forgot to enter the building name!</p>';
} else {
$bn = $_POST['building_name'];
}
if($ln && $fn && $fln && $bn) { //if everthings is ok.
//register the user in the database.
require_once ('connect.php'); // Connect to the db.
// make the query.
$query = "DELETE FROM Residents WHERE (surname, first_name, flat_no, building_name) = ('$ln', '$fn', '$fln', '$bn', '$tn', '$o', '$d', '$r')";
$result = mysql_query ($query); //run the query.
if ($result) { // if it ran ok.
echo '<p><b>Record deleted!</b></p>';
exit(); // Quit the script
} else { // if it didnt run ok.
$message = '<p>Data could not be deleted due to a system error.</p><p>' . mysql_error() . '</p>';
}
mysql_close(); //Close the database connection.
} else {
$message .= '<p>Please try again.</p>';
}
} // End of the main submit conditional.
// Print the message if there is one.
if (isset($message)) {
echo '<font color="red">', $message, '</font>';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset><legend>Enter your information in the form below:</legend>
<p><b>Last Name:</b> <input type="text" name="surname" size="30" maxlength="30"
value="<?php if (isset($_POST['surname'])) echo $_POST['surname']; ?>" /></p>
<p><b>First Name:</b> <input type="text" name="first_name" size="15" maxlength="15"
value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
<p><b>Flat No:</b> <input type="text" name="flat_no" size="15" maxlength="15"
value="<?php if (isset($_POST['flat_no'])) echo $_POST['flat_no']; ?>" /></p>
<p><b>Building Name:</b> <input type="text" name="building_name" size="15" maxlength="15"
value="<?php if (isset($_POST['building_name'])) echo $_POST['building_name']; ?>" /></p>
</fieldset>
<div align="centre"><input type="submit" name"submit" value="Delete Record" /></div>
</form><!-- end of form -->
Currently I am having problems with deleting a complete record from a mysql db. The code below shows that I am requesting the user to enter four fields that will identify the record in the database. In total there are four more fields in each record however I dont want the user to identify them. From this I would like it to locate the record and then delete the entire record.
However im unsure on how the code should be written.
Any help would be appreciated.
Best regards
code:
<?php # Scrtipt 6.6 - register.php
$page_title = 'Add New Entry';
if (isset($_POST['submit'])) { // Handle the form.
$message = NULL; // create an empty new variable.
//check for a surname
if (empty($_POST['surname'])) {
$ln = FALSE;
$message .= '<p>You forgot to enter your surname!</p>';
} else {
$ln = $_POST['surname'];
}
//check for a first name
if (empty($_POST['first_name'])) {
$fn = FALSE;
$message .= '<p>You forgot to enter your first name!</p>';
} else {
$fn = $_POST['first_name'];
}
//check for a flat no
if (empty($_POST['flat_no'])) {
$fln = FALSE;
$message .= '<p>You forgot to enter youe Flat No!</p>';
} else {
$fln = $_POST['flat_no'];
}
//check for a building name
if (empty($_POST['building_name'])) {
$bn = FALSE;
$message .= '<p>You forgot to enter the building name!</p>';
} else {
$bn = $_POST['building_name'];
}
if($ln && $fn && $fln && $bn) { //if everthings is ok.
//register the user in the database.
require_once ('connect.php'); // Connect to the db.
// make the query.
$query = "DELETE FROM Residents WHERE (surname, first_name, flat_no, building_name) = ('$ln', '$fn', '$fln', '$bn', '$tn', '$o', '$d', '$r')";
$result = mysql_query ($query); //run the query.
if ($result) { // if it ran ok.
echo '<p><b>Record deleted!</b></p>';
exit(); // Quit the script
} else { // if it didnt run ok.
$message = '<p>Data could not be deleted due to a system error.</p><p>' . mysql_error() . '</p>';
}
mysql_close(); //Close the database connection.
} else {
$message .= '<p>Please try again.</p>';
}
} // End of the main submit conditional.
// Print the message if there is one.
if (isset($message)) {
echo '<font color="red">', $message, '</font>';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset><legend>Enter your information in the form below:</legend>
<p><b>Last Name:</b> <input type="text" name="surname" size="30" maxlength="30"
value="<?php if (isset($_POST['surname'])) echo $_POST['surname']; ?>" /></p>
<p><b>First Name:</b> <input type="text" name="first_name" size="15" maxlength="15"
value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
<p><b>Flat No:</b> <input type="text" name="flat_no" size="15" maxlength="15"
value="<?php if (isset($_POST['flat_no'])) echo $_POST['flat_no']; ?>" /></p>
<p><b>Building Name:</b> <input type="text" name="building_name" size="15" maxlength="15"
value="<?php if (isset($_POST['building_name'])) echo $_POST['building_name']; ?>" /></p>
</fieldset>
<div align="centre"><input type="submit" name"submit" value="Delete Record" /></div>
</form><!-- end of form -->