Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting an entire record by users request

Status
Not open for further replies.

biscuitboy

Technical User
Dec 11, 2004
22
0
0
GB
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 -->
 
i;m not used to that where syntax. are you sure it is valid?

i would normally construct a where clause like this:

where
`field1` = 'value1'
and
`field2` = 'value2'

 
Hiya.

I would normally construct like that as well however dont you have to have the line "delete from table where field = value?

I might be wrong as im quite new to php interacting with mysql.

How would you construct it??

Best Regards
 
Hiya,

Ok I understand you know, got a bit confused then. What you have suggested does work. However it it wont allow me to delete a whole record based on two fields. Is there any way of doing this as I dont want the user to have to enter all of the fields in the where statement.

best regards
 
Hiya,

I have reorganised some of the code and it now works great.

Thank you for all your help

best regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top