我正在寻找一种使用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();
这不起作用
答案 0 :(得分:1)
您应该可以使用同一个表中针对给定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
临时表插入不起作用的原因是因为主键与数据一起使用,并且在插回主表时无法复制它。
答案 1 :(得分: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();
}