bSky
bSky

Reputation: 187

PHP How to use a Insert Query and a Delete Query in Conjunction

I searched a great deal for a solution to this problem, however i have had no luck in finding an answer, i am trying to archive a row from one table to another and then delete the row from the original table. I have the code set up how i think it should be but it only moves the record it does not delete it from the Original table.

mysql_connect('localhost', 'brokensky', 'password');
mysql_select_db('isad235database');

$select = filter_var($_POST['selectlist'],FILTER_SANITIZE_STRING);

$query = "INSERT INTO `serversarchive` (`Server_ID`,`Server_name` , `Location` , `MAC_Address` , `Port_Number` , `IP_Address`, `Operating_System`, `Administrator`, `Contact_Number`, `Email` , `Second_Contact`, `Second_Contact_Number`, `Comments`)
SELECT * FROM `servers`
WHERE `Server_ID` = '$select'; DELETE FROM server WHERE Server_ID = = '$select'";

As i have looked around for several hours with no decent answer to this, i think this could be useful to others also.

Upvotes: 1

Views: 46

Answers (2)

Riad
Riad

Reputation: 3850

You need to execute sequentially.

$query = "INSERT INTO `serversarchive` (`Server_ID`,`Server_name` , `Location` , `MAC_Address` , `Port_Number` , `IP_Address`, `Operating_System`, `Administrator`, `Contact_Number`, `Email` , `Second_Contact`, `Second_Contact_Number`, `Comments`)
SELECT * FROM `servers`
WHERE `Server_ID` = '$select'"; 

//execute first  

$query2 = "DELETE FROM server WHERE Server_ID = '$select'"; // == should be single =

//now execute this one.

Upvotes: 0

PaulProgrammer
PaulProgrammer

Reputation: 17670

You need to do the steps in sequence. If they need to happen as an atomic unit, use a transaction.

See also: PHP + MySQL transactions examples

Upvotes: 2

Related Questions