Page 1 of 1

php/mysql delete problem

Posted: Fri Aug 06, 2004 6:29 am
by Pizmal
Has anyone else come across this. When i delete a line in mysql with:

delete from workorders where workorder_id=$id

it removes the whole line. Workorder_id and everything; However, in php when i use that query it leaves the workorder_id leaving a blank line. Any ideas as to why i get different results?

From
Pizmal

Posted: Fri Aug 06, 2004 6:35 am
by markl999
Whip up a quick example script and post it here. This is not expected behaviour so it suggests something in your code.

Posted: Fri Aug 06, 2004 6:59 am
by Pizmal
Here is an example. Will post my code when I can (at work). Sorry if its a little choppy just threw it together.

// if someone pushed submit on form either update line or create

if ($submit) {

if ($id) {
$sql = update.....
} ELSE {
$sql = insert.....
}
$result =mysql_query($sql);
echo "your record has been updated or created;

// If they push the delete button remove line
} ELSEIF ($delete) {

$sql = "DELETE FROM workorders WHERE workorder_id="$id";
$result = mysql_query($sql);

} ELSE {

// show section coming from search page....

Posted: Fri Aug 06, 2004 7:04 am
by markl999
if($id){
//update
} else {
//insert
} elseif {
//delete
}
So looks to me like it will do an insert then a delete, which would explain what you are seeing.

You probably want something more along the lines of:

Code: Select all

if(!empty($_POST['delete'])){
  //delete here
} else {
   if(empty($_POST['id'])){
      //insert
   } else {
      //update
   }
}