0

我正在寻找一种使用 PDO 复制这一行的方法。我不想重新输入所有值。

$strsql="INSERT INTO `dreamyAuctions`(`category`,`type`,`city`,`host`,`host_name`,`title`,`fee`,`base_fee`,`min_fee`,`max_fee`,`min_fee2`,`max_fee2`,`last_fee_change`,`seats`,`retail_price`,`asking_price`,`details`,`delivery`,`delivery_time`,`timer`,`start_date`,`free_bids`,`seats_taken`,`show_on_homepage`,`vet_players`,`tournament`,`final_round`,`refresh`,`repost`,`started`,`completed`,`active`,`create_date`,`complete_date`,`minimum_auctions`,`video`,`video2`,`show_pinterest`,`bid_fee`,`saves`,`seat_free`) 
VALUES(\"$cat\",\"$contest_type\",\"$city\",\"$host\",\"$host_name\",'" . mysql_real_escape_string($title) . "',\"$base_fee\",\"$base_fee\",\"$min_fee\",\"$max_fee\",\"$min_fee2\",\"$max_fee2\",now(),\"$seats\",\"$retail\",\"$ask_price\",'" . mysql_real_escape_string($details) . "',\"$delivery\",\"$dtime\",'11',\"$start_date\",\"$free_bids\",'0',\"$show\",\"$vet\",\"$tournament\",'N',\"$refresh\",'Y','N','N','Y',now(),'',\"$min_auctions\",'" . mysql_real_escape_string($video) . "','" . mysql_real_escape_string($video2) . "',\"$pinterest\",\"$bfee\",\"$pre_set_saves\",\"$free_to_seat\")";
mysql_query($strsql,$connect) or die(mysql_error());
$chkrow1=mysql_affected_rows($connect);
$aid=mysql_insert_id();

我试过了

$auctionId=1;
//create new table with old data
$st = $db->prepare("CREATE TEMPORARY TABLE temp_tbl SELECT * FROM `dreamyAuctions` WHERE `id`=:auctionId;
INSERT INTO dreamyAuctions SELECT * FROM temp_tbl;
DROP TABLE temp_tbl;"); // need to filter for next auction
$st->bindParam(':auctionId', $auctionId); // filter
$st->execute();

这是行不通的

4

2 回答 2

2

您应该能够从给定的auction_id 的同一个表中直接选择插入dreamyAuctions - 您只需列出除auction_id 之外的所有字段,因为这是主键(假设)并且不能重复。

INSERT INTO `dreamyAuctions`(`category`,`type`,`city`,`host`,`host_name`,`title`,`fee`,`base_fee`,`min_fee`,`max_fee`,`min_fee2`,`max_fee2`,`last_fee_change`,`seats`,`retail_price`,`asking_price`,`details`,`delivery`,`delivery_time`,`timer`,`start_date`,`free_bids`,`seats_taken`,`show_on_homepage`,`vet_players`,`tournament`,`final_round`,`refresh`,`repost`,`started`,`completed`,`active`,`create_date`,`complete_date`,`minimum_auctions`,`video`,`video2`,`show_pinterest`,`bid_fee`,`saves`,`seat_free`) 
SELECT
`category`,`type`,`city`,`host`,`host_name`,`title`,`fee`,`base_fee`,`min_fee`,`max_fee`,`min_fee2`,`max_fee2`,`last_fee_change`,`seats`,`retail_price`,`asking_price`,`details`,`delivery`,`delivery_time`,`timer`,`start_date`,`free_bids`,`seats_taken`,`show_on_homepage`,`vet_players`,`tournament`,`final_round`,`refresh`,`repost`,`started`,`completed`,`active`,`create_date`,`complete_date`,`minimum_auctions`,`video`,`video2`,`show_pinterest`,`bid_fee`,`saves`,`seat_free`
FROM `dreamyAuctions` WHERE auction_id=:auctionId

您的临时表插入不起作用的原因是因为主键与数据一起使用,并且在插入回主表时无法复制它。

于 2013-08-06T18:46:53.917 回答
1

要查看 PDO 的错误,请使用您的代码查看此示例:

// Connect to MySQL via PDO
try {
$db = new PDO("mysql:host=localhost;port=3306;dbname=users_db", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

// try to execute the query, if there is a error it goes to the catch block.
try {
$st = $db->prepare("CREATE TEMPORARY TABLE temp_tbl SELECT * FROM `dreamyAuctions` WHERE `id`=:auctionId;
INSERT INTO dreamyAuctions SELECT * FROM temp_tbl;
DROP TABLE temp_tbl;"); // need to filter for next auction
$st->bindParam(':auctionId', $auctionId); // filter
$st->execute();
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
于 2013-08-06T18:47:16.667 回答