mysql - Getting output from a PHP shuffle and INSERT into new database table -


i have code below shuffles output of users in 'users' table. want able send shuffled results new db table in order shuffle sorted results. wanting each sorted result create new row in db.

i'm not sure how can grab results of php shuffle , send it. added in new submit input, when results have been shown, user can submit results. results can not inserted during shuffling process, has after results have been outputted.

how can results shuffle , insert them? want see how can current results.

$con = mysqli_connect("localhost", "", "", "db"); $query = mysqli_query($con, "select * users `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 $result) {     $shuffle_firstname = $result['firstname'];     $shuffle_lastname = $result['lastname']; ?>  <div id="shuffle_results">         <?php echo $shuffle_firstname . ' ' . $shuffle_lastname . '<br>';?> </div>  <?php   } }  //what new submit button  <form method="post">         <input type="submit" value="insert db" name="insert">     </form> 

update: added prepared statement

<?php $con = mysqli_connect("localhost", "root", "", "db"); $query = mysqli_query($con, "select * users `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 $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 $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 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 user_players (user_id, firstname, lastname, username, email) values (?, ?, ?, ?, ?)");     if ( false===$stmt2 ) {          // check errors prepare         die('add 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 binding parameters         die('add user players bind_param() failed: ' . htmlspecialchars($stmt2->error));     }     $stmt2->execute();     if ( false===$stmt2 ) {         die('add user players execute() failed: ' . htmlspecialchars($stmt2->error));     } } 

put shuffled names in <input type="hidden"> fields in form.

if (isset($_post['shuffle'])) {     shuffle($array);     ?>     shuffled results:<br>     <form method="post">     foreach ($array $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 db" name="insert">     </form> } 

when form submitted, $_post['firstname'] , $_post['lastname'] arrays, can loop through them , insert them new table.

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 user_players (user_id, firstname, lastname, username, email) values (?, ?, ?, ?, ?)");     if ( false===$stmt2 ) {          // check errors prepare         die('add user players prepare() failed: ' . htmlspecialchars($con->error));     }     $stmt2->bind_param('issss', $shuffle_id, $shuffle_firstname, $shuffle_lastname, $shuffle_username, $shuffle_email);      foreach ($_post['id'] $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 user players execute() failed: ' . htmlspecialchars($stmt2->error));     } } 

note should use class rather id shuffle_results div, because you're adding 1 each row in results. ids have unique.

you put $result['id'] in insert form, without other hidden inputs. script processes can other fields users table , inser them user_players.

$stmt2 = $con->prepare("insert user_players (user_id, firstname, lastname, username, email)                         select user_id, firstname, lastname, username, email                         users                          id = ?"); $stmt2->bind_param("i", $shuffle_id); foreach ($_post['id'] $shuffle_id) {     $stmt2->execute() or die('add user players execute() failed: ' . htmlspecialchars($stmt2->error)); } 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -