"Moving" an entry from one table to another

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

"Moving" an entry from one table to another

Post by a94060 »

Hi,i was wondering what would be the best way to move a recored from one table to another one:

i have thought of this way so far,but i think there maybe a more efficent way?

Code: Select all

$act = $_GET['act'];
$entry = $_GET['entry'];//the id of the entry
if($act == 'approve') {
$query = "SELECT * FROM poffers WHERE id=$entry";
$result = mysql_query($query);
$data = mysql_fetch_array($result,MYSQL_ASSOC);
$sql2 = "INSERT INTO coffers (username,offer_name) VALUES ('$data['username']' , '$data['offer_name']' , '$data['offer_type']')";
$sql2run = mysql_query($sql2);
//remove it from the old table
$delete = "DELETE FROM poffers WHERE id=$entry LIMIT 1";
$delrun = mysql_query($delete);
$hits = mysql_num_rows($delrun);
if($hits == '1') {
echo 'The offer' .$entry.'has been removed from pending offers and has been moved to completed offers.';
}
}
}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

That is the basics if all goes well. At least you should check to see if the INSERT was successful before doing the DELETE. Better would be to make it all a transaction.
(#10850)
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

You mean like this:

Code: Select all

<?PHP
include "../includes/session.php";
else {
/*the action page.all actions of the admin will be redirected here.
That is all*/
$act = $_GET['act'];
$entry = $_GET['entry'];//the id of the entry
if($act == 'approve') {
$query = "SELECT * FROM poffers WHERE id=$entry";
$result = mysql_query($query);
$data = mysql_fetch_array($result,MYSQL_ASSOC);
$sql2 = "INSERT INTO coffers (username,offer_name) VALUES ('$data['username']' , '$data['offer_name']' , '$data['offer_type']')";
$sql2run = mysql_query($sql2);//inserts the offer to completed table.
//remove it from the old table
$put = mysql_num_rows($sql2run);
/*This is what i added in about what you said*/
if($put == 1) {
$delete = "DELETE FROM poffers WHERE id=$entry LIMIT 1";
$delrun = mysql_query($delete);
$hits = mysql_num_rows($delrun);
if($hits == '1') {
echo 'The offer' .$entry.'has been removed from pending offers and has been moved to completed offers.';
}
}
}
}
Post Reply