0

I have a file where I had two queries. One that selected the data from a table and shuffled it, then the second query inserts the shuffled results into another database of mine called user_players.

It all worked perfectly until I changed my first query. I moved it to another file and structured it with an AJAX call. Now my second query is breaking and it will not send to my database. I am getting errors with undefined index and missing variables, but I have all of the variables included. What could be wrong with this? If anyone needs to see the first query, as that is where I get the info, please let me know.

This is what is failing and I am getting the following errors:

Notice: Undefined index: id in /home4/dhgh/public_html/example.com/userCreator.php on line 235

Warning: Invalid argument supplied for foreach() in /home4/gddgg/public_html/example.com/userCreator.php on line 235

LINE 235:

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

My code for this second query.

<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));
    }
baao
  • 71,625
  • 17
  • 143
  • 203
Paul
  • 3,348
  • 5
  • 32
  • 76
  • `$_POST['id']` is not an array. `$_POST` is but as you're selecting an key from the array it becomes whatever the variable value type it is. The undefined index error means that `id` has not been set. – Script47 Aug 07 '15 at 14:28
  • How isn't it set? I have it defined right here `$shuffle_id = htmlentities($result['id']);` – Paul Aug 07 '15 at 14:32

0 Answers0