Jump to content

Deleting an entry in a database table.


vomitbomb

Recommended Posts

  <?
//make the database connection
$conn  = mysql_connect("localhost", "", "");
mysql_select_db("example", $conn);

$search = $_POST['search'];

//create a query
$sql = "SELECT * FROM orders WHERE lname='$search'";
$result = mysql_query($sql, $conn);
$row = mysql_fetch_array( $result );

echo "<h3>Showing Order for: </h3>"; 

echo "" .$row['fname'];

echo "" .$row['lname'];

echo "<br>";

if($row['pnumber'] != 0){
echo "Home Phone: ".$row['pnumber'];
echo "<br>";
}

if($row['mnumber'] != 0){
echo "Mobile Phone: ".$row['mnumber'];
echo "<br>";
}

mysql_close($conn);

?>

 

Hmm.. Anyone know how I can put a button, form or link in there to execute the:

DELTE FROM some_table WHERE <conditions>

To delete this order?

yep, the one that has come up in the search result, thats the row I wanna delete, I think I've worked a way around it but having 2 buttons, one goes to a script that deletes the order, one just goes to the main page.

 

Don't know if there's a better way to do it.

 

How do I take the $row variable from this script to another, so I can delete it?

Hope this helps you, if you want to delete the row on the another page I suppose, you have to send the value of the row with sessions..

 

 <?php
//make the database connection
$conn  = mysql_connect("localhost", "", "");
mysql_select_db("example", $conn);

$search = $_POST['search'];

//create a query
$sql = "SELECT * FROM orders WHERE lname='{$search}'";
$result = mysql_query($sql, $conn);
$row = mysql_fetch_array( $result );

echo "<h3>Showing Order for: </h3>"; 

echo "" .$row['fname'];

echo "" .$row['lname'];

echo "<br>";

if($row['pnumber'] != 0){
echo "Home Phone: ".$row['pnumber'];
echo "<br>";
}

if($row['mnumber'] != 0){
echo "Mobile Phone: ".$row['mnumber'];
echo "<br>";
}

// deleting the selected row
if(!empty($row['fname']) && !empty($row['lname'])){ //check if fname and lname are not empty..
$sql = "DELETE FROM orders WHERE fname='" . $row["fname"] . "' AND lname='" . $row["lname"]."' LIMIT 1"; //delete row with fname and lname selected from the row 

$result = mysql_query($sql,$conn); //execute the query..

if($result){		
 echo "<p>Row with fname: " . $row['fname'] . " has been deleted!</p>"; //send info to user 
}else {
     echo "<p>Error has occured!</p>"; //otherwise print error..
}

}else{
die('fname and lname must not be empty!');
}
    	

mysql_close($conn);

?>

I'll have to try that, but I need it in the form of a button, I don't want the orders being deleted everytime someone looks at them.

 

Can I parse the variable $row from this script to another using a form? if so how do I do that?

 

Basically I want to parse the result (using a button or link on this page) that came up into a document that instead of looking at it's contents, deletes the entry(row).

 

I know how to parse a value from a form, but how about a value inside a variable thats already been parsed?  ???

In a real order processing application, you don't delete orders from the database when they are complete. You change a status column to indicate that they are complete. In this way you have a record of the order (who, what, when, where...), in case they want to return an item or you need to do something like detect credit card abuse...

Ok my new question is, How do I take the $row variable from that script to another? I've only used html forms to post values into variables.

 

My search page opens up the code above, I then want to use the users details from that script in another script to update the records.

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.