gpt4 book ai didi

php - 从 PHP 洗牌中获取输出并将其插入到新的数据库表中

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

我有下面的代码,可以在我的“用户”表中打乱用户的输出。我希望能够按照洗牌对结果进行排序的顺序将洗牌后的结果发送到新的数据库表。我希望每个排序结果在我的数据库中创建一个新行。

我不确定如何获取 PHP 洗牌的结果并发送它。我添加了一个新的提交输入,所以当显示结果时,用户可以提交结果。洗牌过程中不能插入结果,必须在结果输出后插入。

如何从随机播放中获取结果并将其插入?我只想看看如何获​​得当前结果。

$con = mysqli_connect("localhost", "", "", "db");
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3");

echo 'Normal results: <br>';
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
$array[] = $row;
echo $row['firstname'] . ' ' . $row['lastname'] . '<br>';
}
?>
<form method="post">
<input type="submit" value="Shuffle" name="shuffle">
</form>
<?php
if (isset($_POST['shuffle'])) {
shuffle($array);
echo 'Shuffled results: <br>';
foreach ($array as $result) {
$shuffle_firstname = $result['firstname'];
$shuffle_lastname = $result['lastname'];
?>

<div id="shuffle_results">
<?php echo $shuffle_firstname . ' ' . $shuffle_lastname . '<br>';?>
</div>
<?php }
}
//What the new submit button will be like
<form method="post">
<input type="submit" value="Insert to db" name="insert">
</form>

更新:添加了准备好的声明

<?php
$con = mysqli_connect("localhost", "root", "", "db");
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3");

echo 'Normal results: <br>';
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
$array[] = $row;
echo $row['firstname'] . ' ' . $row['lastname'] . '<br>';
}
?>
<form method="POST">
<input type="submit" value="Shuffle" name="shuffle">
</form>
<?php
if (isset($_POST['shuffle'])) {
shuffle($array);
echo 'Shuffled results: <br>';
foreach ($array as $result) {
$shuffle_id = $result['id'];
$shuffle_firstname = $result['firstname'];
$shuffle_lastname = $result['lastname'];
?>

<div id="shuffle_results">
<?php echo '<ol>' . '<li>' . $shuffle_firstname . ' ' . $shuffle_lastname . '</li>' . '</ol>' . '<br>';?>
</div>
<?php }

}
($user->lastId);
var_dump($user->insert_id);

if (isset($_POST['shuffle'])) {
shuffle($array);
?>
Shuffled results:<br>
<form method="post">
<?php
foreach ($array as $result) {
$shuffle_id = htmlentities($result['id']);
$shuffle_firstname = htmlentities($result['firstname']);
$shuffle_lastname = htmlentities($result['lastname']);
$shuffle_username = htmlentities($result['username']);
$shuffle_email = htmlentities($result['email']);

?>
<div class="shuffle_results"><?php echo $shuffle_firstname . ' ' . $shuffle_lastname; ?></div>
<input type="hidden" name="firstname[]" value="<?php echo $shuffle_firstname; ?>">
<input type="hidden" name="lastname[]" value="<?php echo $shuffle_lastname; ?>">
<?php
}
?>
<input type="submit" value="Insert to db" name="insert">
</form>
<?php
$con = mysqli_connect("localhost", "root", "", "db");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt2 = $con->prepare("INSERT INTO user_players (user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?)");
if ( false===$stmt2 ) {
// Check Errors for prepare
die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
}
$stmt2->bind_param('issss', $shuffle_id, $shuffle_firstname, $shuffle_lastname, $shuffle_username, $shuffle_email);
if ( false===$stmt2 ) {
// Check errors for binding parameters
die('Add to user players bind_param() failed: ' . htmlspecialchars($stmt2->error));
}
$stmt2->execute();
if ( false===$stmt2 ) {
die('Add to user players execute() failed: ' . htmlspecialchars($stmt2->error));
}
}

最佳答案

将打乱的名字放入<input type="hidden">表单中的字段。

if (isset($_POST['shuffle'])) {
shuffle($array);
?>
Shuffled results:<br>
<form method="post">
foreach ($array as $result) {
$shuffle_firstname = htmlentities($result['firstname']);
$shuffle_lastname = htmlentities($result['lastname']);
$shuffle_id = htmlentities($result['id']);
$shuffle_username = htmlentities($result['username']);
$shuffle_email = htmlentities($result['email']);

?>
<div class="shuffle_results"><?php echo $shuffle_firstname . ' ' . $shuffle_lastname; ?></div>
<input type="hidden" name="firstname[]" value="<?php echo $shuffle_firstname; ?>">
<input type="hidden" name="lastname[]" value="<?php echo $shuffle_lastname; ?>">
<input type="hidden" name="id[]" value="<?php echo $shuffle_id; ?>">
<input type="hidden" name="username[]" value="<?php echo $shuffle_username; ?>">
<input type="hidden" name="email[]" value="<?php echo $shuffle_email; ?>">
<?php
}
<input type="submit" value="Insert to db" name="insert">
</form>
}

提交此表单后,$_POST['firstname']$_POST['lastname']将是数组,因此您可以遍历它们并将它们插入到新表中。

if (isset($_POST['insert'])) {
$con = mysqli_connect("localhost", "root", "", "db");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt2 = $con->prepare("INSERT INTO user_players (user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?)");
if ( false===$stmt2 ) {
// Check Errors for prepare
die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
}
$stmt2->bind_param('issss', $shuffle_id, $shuffle_firstname, $shuffle_lastname, $shuffle_username, $shuffle_email);

foreach ($_POST['id'] as $i => $shuffle_id) {
$shuffle_firstname = $_POST['firstname'][$i];
$shuffle_lastname = $_POST['lastname'][$i];
$shuffle_username = $_POST['username'][$i];
$shuffle_email = $_POST['email'][$i];
$stmt2->execute() or
die('Add to user players execute() failed: ' . htmlspecialchars($stmt2->error));
}
}

请注意,您应该为 shuffle_results 使用类而不是 ID DIV,因为您要为结果中的每一行添加一个。 ID 必须是唯一的。

你也可以只输入 $result['id']insert形式,没有所有其他隐藏的输入。然后处理它的脚本可以从 users 中获取其他字段。表并将它们插入 user_players .

$stmt2 = $con->prepare("INSERT INTO user_players (user_id, firstname, lastname, username, email)
SELECT user_id, firstname, lastname, username, email
FROM users
WHERE id = ?");
$stmt2->bind_param("i", $shuffle_id);
foreach ($_POST['id'] as $shuffle_id) {
$stmt2->execute() or die('Add to user players execute() failed: ' . htmlspecialchars($stmt2->error));
}

关于php - 从 PHP 洗牌中获取输出并将其插入到新的数据库表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31467175/

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