打乱后的结果未按打乱后的顺序发送到数据库
Shuffled results not sending to database in the shuffled order
我有一个页面正在重新排列用户订单。我想将该订单的结果发送到我的数据库中,以便我以后可以将该订单用于某些事情。我认为结果是按照它们在洗牌时出现的顺序发送的,但事实并非如此,我不确定它们发送的确切顺序。
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` IN (3, 4 ,5)");
echo 'Users to be given draft order: <br>';
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
$array[] = $row;
echo $row['firstname'] . ' ' . $row['lastname'] . '<br>';
}
?>
<form method="
POST" name="form">
<input type="submit" value="Create Draft Order" name="shuffle">
<input type="hidden" name="shuffleButton" value="1">
</form>
Shuffled results: <br>
<div class="main-bag">
<div class="shuffle_results" id="results"></div>
<form method="post">
<input type="submit" value="Finalize Draft Order" name="insert">
<?php
$count = 0;
foreach ($array as $result) :
$count++;
$shuffle_count = $count;
$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']);
?>
<input type="hidden" name="count[]" value="<?php echo $shuffle_count; ?>">
<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
endforeach;
if ( isset($_POST['shuffleButton'] ) ) :
echo '<input type="submit" value="Finalize Draft Order" name="insert">';
endif;
?>
</form>
<?php
if (isset($_POST['insert'])) {
$ordering_number = "1";
$con = mysqli_connect("localhost", "", "", "");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$draft_stmt1 = $con->prepare("INSERT INTO user_players (user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?)");
if ( false===$draft_stmt1|| false===$draft_stmt2 ) {
// Check Errors for prepare
die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
}
$draft_stmt1->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];
$draft_stmt1->execute() or
die('Add to user players execute() failed: ' . htmlspecialchars($draft_stmt1->error));
有谁知道为什么这不会按照我正在洗牌的顺序发送?我的数据库中有一个基本 ID 字段 table,这样每个插入的用户都会按照他们被起草的顺序获得 ID,但它不是那样工作的。
抱歉所有代码,我想确保不要忘记可能有用的东西谢谢!
更新
新代码
$draft_stmt1 = $con->prepare("INSERT INTO user_players (id, user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?, ?)");
if ( false===$draft_stmt1|| false===$draft_stmt2 || false===$draft_stmt3) {
// Check Errors for prepare
die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
}
$draft_stmt1->bind_param('iissss', $i, $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];
$draft_stmt1->execute() or
die('Add to user players execute() failed: ' . htmlspecialchars($draft_stmt1->error));
这是用户被洗牌的地方..
$query = mysqli_query($con, "SELECT * FROM users WHERE group IN (3,4,5) ORDER BY id");
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
$array[] = array(
'id' => $row['id'],
'firstname' => $row['firstname'],
'lastname' => $row['lastname'],
'username' => $row['username'],
'email' => $row['email']
);
if (isset($_POST['shuffle'])) {
}
}
shuffle($array);
echo json_encode($array);
向 table 添加一列 shuffle_order
,并在该列中存储从表单中收到玩家的顺序。
$draft_stmt1 = $con->prepare("INSERT INTO user_players (shuffle_order, user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?, ?)");
if ( false===$draft_stmt1|| false===$draft_stmt2 ) {
// Check Errors for prepare
die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
}
$draft_stmt1->bind_param('iissss', $i, $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];
$draft_stmt1->execute() or
die('Add to user players execute() failed: ' . htmlspecialchars($draft_stmt1->error));
}
然后当你想按打乱顺序处理数据时,在SELECT
查询中使用ORDER BY shuffle_order
。
我有一个页面正在重新排列用户订单。我想将该订单的结果发送到我的数据库中,以便我以后可以将该订单用于某些事情。我认为结果是按照它们在洗牌时出现的顺序发送的,但事实并非如此,我不确定它们发送的确切顺序。
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` IN (3, 4 ,5)");
echo 'Users to be given draft order: <br>';
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
$array[] = $row;
echo $row['firstname'] . ' ' . $row['lastname'] . '<br>';
}
?>
<form method="
POST" name="form">
<input type="submit" value="Create Draft Order" name="shuffle">
<input type="hidden" name="shuffleButton" value="1">
</form>
Shuffled results: <br>
<div class="main-bag">
<div class="shuffle_results" id="results"></div>
<form method="post">
<input type="submit" value="Finalize Draft Order" name="insert">
<?php
$count = 0;
foreach ($array as $result) :
$count++;
$shuffle_count = $count;
$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']);
?>
<input type="hidden" name="count[]" value="<?php echo $shuffle_count; ?>">
<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
endforeach;
if ( isset($_POST['shuffleButton'] ) ) :
echo '<input type="submit" value="Finalize Draft Order" name="insert">';
endif;
?>
</form>
<?php
if (isset($_POST['insert'])) {
$ordering_number = "1";
$con = mysqli_connect("localhost", "", "", "");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$draft_stmt1 = $con->prepare("INSERT INTO user_players (user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?)");
if ( false===$draft_stmt1|| false===$draft_stmt2 ) {
// Check Errors for prepare
die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
}
$draft_stmt1->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];
$draft_stmt1->execute() or
die('Add to user players execute() failed: ' . htmlspecialchars($draft_stmt1->error));
有谁知道为什么这不会按照我正在洗牌的顺序发送?我的数据库中有一个基本 ID 字段 table,这样每个插入的用户都会按照他们被起草的顺序获得 ID,但它不是那样工作的。
抱歉所有代码,我想确保不要忘记可能有用的东西谢谢!
更新
新代码
$draft_stmt1 = $con->prepare("INSERT INTO user_players (id, user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?, ?)");
if ( false===$draft_stmt1|| false===$draft_stmt2 || false===$draft_stmt3) {
// Check Errors for prepare
die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
}
$draft_stmt1->bind_param('iissss', $i, $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];
$draft_stmt1->execute() or
die('Add to user players execute() failed: ' . htmlspecialchars($draft_stmt1->error));
这是用户被洗牌的地方..
$query = mysqli_query($con, "SELECT * FROM users WHERE group IN (3,4,5) ORDER BY id");
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
$array[] = array(
'id' => $row['id'],
'firstname' => $row['firstname'],
'lastname' => $row['lastname'],
'username' => $row['username'],
'email' => $row['email']
);
if (isset($_POST['shuffle'])) {
}
}
shuffle($array);
echo json_encode($array);
向 table 添加一列 shuffle_order
,并在该列中存储从表单中收到玩家的顺序。
$draft_stmt1 = $con->prepare("INSERT INTO user_players (shuffle_order, user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?, ?)");
if ( false===$draft_stmt1|| false===$draft_stmt2 ) {
// Check Errors for prepare
die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
}
$draft_stmt1->bind_param('iissss', $i, $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];
$draft_stmt1->execute() or
die('Add to user players execute() failed: ' . htmlspecialchars($draft_stmt1->error));
}
然后当你想按打乱顺序处理数据时,在SELECT
查询中使用ORDER BY shuffle_order
。