在我的基于PHP / MYSQL的网站上,卖家可以发布10种相同类型,性质和价值的商品。
我希望用户能够发布qty = 10,value = 100.(值在下拉列表中预设并由卖家选择。)
目前,我只能为预设值发布单个项目,并生成代码,如下面的代码所示:
$sql_query = "INSERT INTO
eg_posts(seller_id,value_id,quantity,cdate,egcode)
VALUES
('$a','$bval','$cqty',now(),CONCAT( CHAR(FLOOR(RAND()*26)+65),FLOOR(100+RAND()*(5000-100))))";
以上帖子与一个项目一起保存,其中一个字母数字代码带有卖家选择的预设值。
我希望用户能够发布多个相同类型,性质和价值的项目(比如说10个),我希望为这10个项目生成10个随机字母数字代码。我希望所有10个随机代码都保存在帖子ID下的数据库中。
答案 0 :(得分:1)
使用PDO(即使您想要更改不同值,数量的值也会有效,因为它会分别绑定每个变量):
try {
//Make your connection handler to your database
$conn = new PDO("mysql:host=".$servername.";dbname=".$database, $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
$a = 1;
$bval= 1;
$cqty = 10;
$values = array();
$binds = array();
$temp = "INSERT INTO eg_posts (seller_id, value_id, quantity, cdate, egcode) VALUES ";
for($i = 0; $i < 10; $i ++) {
$values[] = ' (seller_id'.$i.' = :seller_id'.$i.', value_id'.$i.' = :value_id'.$i.', quantity'.$i.' = :quantity'.$i.', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) )))';
$binds[':seller_id'.$i] = $a;
$binds[':value_id'.$i] = $bval;
$binds[':quantity'.$i] = $cqty;
}
$sql_query = $temp.implode(', ', $values).';';
$stmt = $conn->prepare($sql_query);
$stmt->execute($binds);
} catch(PDOException $e) {
echo $e->getMessage();
die();
}
结果:
INSERT INTO eg_posts (seller_id, value_id, quantity, cdate, egcode)
VALUES
(seller_id0 = :seller_id0, value_id0 = :value_id0, quantity0 = :quantity0, NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
(seller_id1 = :seller_id1, value_id1 = :value_id1, quantity1 = :quantity1, NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
(seller_id2 = :seller_id2, value_id2 = :value_id2, quantity2 = :quantity2, NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
(seller_id3 = :seller_id3, value_id3 = :value_id3, quantity3 = :quantity3, NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
(seller_id4 = :seller_id4, value_id4 = :value_id4, quantity4 = :quantity4, NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
(seller_id5 = :seller_id5, value_id5 = :value_id5, quantity5 = :quantity5, NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
(seller_id6 = :seller_id6, value_id6 = :value_id6, quantity6 = :quantity6, NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
(seller_id7 = :seller_id7, value_id7 = :value_id7, quantity7 = :quantity7, NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
(seller_id8 = :seller_id8, value_id8 = :value_id8, quantity8 = :quantity8, NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
(seller_id9 = :seller_id9, value_id9 = :value_id9, quantity9 = :quantity9, NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) )));
纯SQL:
$a = 1;
$bval= 1;
$cqty = 10;
$temp = "INSERT INTO eg_posts (seller_id, value_id, quantity, cdate, egcode) VALUES ";
$values = array();
for($i = 0; $i < 10; $i ++) {
$values[] = " ('$a', '$bval', '$cqty', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) )))";
}
$sql_query = $temp.implode(', ', $values).';';
echo $sql_query;
结果:
INSERT INTO eg_posts (seller_id, value_id, quantity, cdate, egcode)
VALUES
('1', '1', '10', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
('1', '1', '10', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
('1', '1', '10', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
('1', '1', '10', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
('1', '1', '10', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
('1', '1', '10', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
('1', '1', '10', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
('1', '1', '10', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
('1', '1', '10', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) ))),
('1', '1', '10', NOW(), CONCAT( CHAR(FLOOR(RAND()*26) + 65), FLOOR(100 + RAND() * (5000-100) )));