Ralph
Ralph

Reputation: 307

Query not inserting into database

I have two queries in the code I am showing. The first one gets users from my database users and then shuffles the results. The second one, I am trying to INSERT the shuffled results into the database user_players. However, my second query is not doing anything.

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

Where it stops working and doesn't send to database

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

An error I get is this..

Warning: Invalid argument supplied for foreach() 

for this line

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

What am I doing wrong and why is that argument invalid?

When I put the </form> and submit button after the inputs, the submit button gets put inside of my loop..

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

Upvotes: 1

Views: 124

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94642

I am not sure what you actually did in response to my comment but this I believe should make the <form> contain all the required html input and the button.

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

Upvotes: 2

mario.van.zadel
mario.van.zadel

Reputation: 2949

$con is the link identifier. You have to pass it to mysqli_prepare method.

Please call

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

instead of

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

Upvotes: 0

Related Questions