0

I changed some of my code in the following file to JS and made an AJAX call in order to make animation easier. However, since I moved part of the PHP to another file and made the AJAX call, I'm having issues with my second query.

What I am trying to do is to display usernames. Then once you click a button it shuffles those usernames and displays them. This all works.

What my second query does is get that info and sends it to a different database table. This is what is failing and I am getting the following errors:

Notice: Undefined index: id in /home4/pfarley1/public_html/sundayfundayleague.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) {

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home4/gfdgd/public_html/example.com/userCreator.php on line 245

LINE 245

$order_row = mysqli_fetch_assoc($stmt);


$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="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

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

JS for getting the Shuffle results from my php file

   $('#results').append('<div class="result">' +
                        '<div class="shuffle_results">' + data[i].firstname + ' ' + data[i].lastname + '</div>' + 
                        '<input type="hidden" name="firstname[]" value="' + data[i].firstname + '">' +
                        '<input type="hidden" name="lastname[]" value="' + data[i].lastname + '">' +
                        '<input type="hidden" name="id[]" value="' + data[i].id + '">' +
                        '<input type="hidden" name="username[]" value="' + data[i].username + '">' +
                        '<input type="hidden" name="email[]" value="' + data[i].email + '">' +
                    '</div>');
           var $this = $('.shuffle_results:last'); 
    $this.show().animate({ 
    'left': 0 + 'px', 
    'bottom': + '0px' 
    //$(document).height() - (lineheight * data.length)
    }, { 
    duration: time 
    });
     i++;
       } else { 
    clearInterval(interval); 
    } 
    }, 3000); 
    };
$(function(){  
    $('form[name="form"]').on('submit', function(e){
        e.preventDefault();
       $.post('shuffle_results.php', function(data){ 
        var o = $.parseJSON(data); 
        displayResults(o); 
        });
        });

PHP file

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

while ($row = mysqli_fetch_assoc($query)) { 
$array[] = array( 
'firstname' => $row['firstname'], 
'lastname' => $row['lastname'], 
'id' => $row['id'], 
'username' => $row['username'], 
'email' => $row['email'] 
); 

if (isset($_POST['shuffle'])) {

    }
}
shuffle($array);
    echo json_encode($array);

Does anyone have any idea why this would be failing since I moved the php code to another file and am doing an AJAX call now?

How can I get those variables.. They were the same as they were before? I just moved things around.

Paul
  • 3,348
  • 5
  • 32
  • 76
  • @john Conde The variables were the same as they were before? I just moved things around. My question is very specific to what I did different – Paul Aug 05 '15 at 19:16
  • That error is pretty clear as to what's wrong and that canonical questions shows how to solve it. Also, there is way too much code here to be a good question. You need to narrow it down a lot more to a the actual problematic code. – John Conde Aug 05 '15 at 19:17

0 Answers0