从PHP shuffle和INSERT获取输出到新的数据库表

时间:2015-07-17 01:39:44

标签: php mysql forms insert

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

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

如何从shuffle获取结果并插入它们?我只想知道如何获得目前的结果。

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

1 个答案:

答案 0 :(得分:2)

将随机播放的名称放在表单中的<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 DIV的类而不是ID,因为您要为结果中的每一行添加一个。 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));
}