gpt4 book ai didi

php - 生成随机数并保存在数据库中

转载 作者:行者123 更新时间:2023-11-29 11:54:06 25 4
gpt4 key购买 nike

在我的 PHP/MYSQL BASED 网站上,卖家可以发布 10 件相同种类、性质和值(value)的商品。

我希望用户能够发布数量 = 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))))";

以上帖子与一件商品一起保存,并带有一个字母数字代码和卖家选择的预设值。

我希望用户能够发布多个相同种类、性质和值(value)的项目(例如 10 个),并且我希望为这 10 个项目生成 10 个随机字母数字代码。我希望所有 10 个随机代码都保存在数据库中的帖子 ID 下。

最佳答案

使用 PDO(即使您想更改不同值、数量的值,它也可以工作,因为它单独绑定(bind)每个变量):

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) )));

关于php - 生成随机数并保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33502712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com