查询未插入数据库

时间:2015-08-12 20:23:10

标签: php mysql sql forms prepared-statement

我在显示的代码中有两个查询。第一个从我的数据库用户获取用户,然后洗牌结果。第二个,我试图将洗牌后的结果插入到数据库user_players中。但是,我的第二个查询没有做任何事情。

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

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">
</form>

    Shuffled results: <br>
    <div class="main-bag">     
    <div class="shuffle_results" id="results"></div>
         <img id='paperBag' src="http://www.thecuriouscaterpillar.co.uk/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/a/bag_to_white.jpg" width="200px" />  

停止工作但不发送到数据库的地方

    <form method="post">
         <input type="submit" value="Finalize Draft Order" name="insert">
    </form>

 <?php
      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']);
        ?>
        <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 
}        
if (isset($_POST['insert'])) {
    $con = mysqli_connect("localhost", "", "", "");
    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));

我得到的错误就是这个..

Warning: Invalid argument supplied for foreach() 

这一行

foreach ($_POST['id'] as $i => $shuffle_id) {

我做错了什么,为什么这个论点无效?

当我在输入后放置</form>和提交按钮时,提交按钮会被放入我的循环中。

<form method="post">




<?php
      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']);
        ?>
        <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; ?>">
        <input type="submit" value="Finalize Draft Order" name="insert">
           </form>

2 个答案:

答案 0 :(得分:2)

我不确定您在回复我的评论时实际做了什么,但我相信这应该使<form>包含所有必需的html输入和按钮。

    <form method="post">
<?php
    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']);
?>
        <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;
    // only show this button if we have done a shuffle
    if ( isset($_POST['shuffle'] ) ) :
        echo '<input type="submit" value="Finalize Draft Order" name="insert">';
    endif;
?>            
    </form>

<?php
if (isset($_POST['insert'])) {
    $con = mysqli_connect("localhost", "", "", "");
    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));

答案 1 :(得分:0)

$con是链接标识符。您必须将其传递给mysqli_prepare方法。

请致电

$stmt2 = mysqli_prepare($con, "INSERT INTO user_players (user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?)");

而不是

$stmt2 = $con->prepare("INSERT INTO user_players (user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?)");