php - Fetch id from Mysqli -


i've been trying add modal popup whenever click button update records have stored in database i'm having trouble getting id

    <?php                                          //open new connection mysql server                                         $mysqli = new mysqli('localhost','root','','pizzeria');                                          //output connection error                                         if ($mysqli->connect_error) {                                         die('error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);                                         }                                         //mysqli select query                                         $results = $mysqli->query("select id, navn, indeholder, pris pizza");                                         print '<tr>';                                         while($row = $results->fetch_assoc()) {                                             print '<td>'.$row["id"].'</td>';                                             print '<td>'.$row["navn"].'</td>';                                             print '<td>'.$row["indeholder"].'</td>';                                             print '<td>'.$row["pris"].'</td>';                                             print '<td align="center"><button type="btn btn-default" data-toggle="modal" data-target="#<?php echo $row['id']; ?>">redigere</button></td>';                                             print '</tr>';                                                                                       }                                         // frees memory associated result                                         $results->free();                                          // close connection                                          $mysqli->close();                                          ?>                                     </tbody>                                 </table>                             </div> <div class="modal fade" id="delete-<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="mymodallabel" aria-hidden="true">   <div class="modal-dialog">     <div class="modal-content">       <div class="modal-header">         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>         <h4 class="modal-title" id="mymodallabel">bekræft venligst sletning</h4>       </div>       <div class="modal-body">         er du sikker på @ du vil slette <?php echo $rows['navn']; ?>?       </div>       <div class="modal-footer">         <button type="button" class="btn btn-default" data-dismiss="modal">luk</button>         <a href="delete.php?id=<?php echo $rows['id']; ?>" class="btn btn-info btn-sm">slet</a>       </div>     </div>   </div> </div>   

is there can fix it?

you're trying call $row variable outside of while loop it's defined in. need assign array values $row new variables while in loop. then, you'll able reference values in second half of script.

$id_array = array(); $nav_array = array(); $indeholder_array = array(); $pris_array = array();  while($row = $results->fetch_assoc()) {      // save results our globally scoped arrays     array_push($id_array, $row["id"]);     array_push($nav_array, $row["navn"]);     array_push($indeholder_array, $row["indeholder"]);     array_push($pris_array, $row["pris"]);      echo '<td>'.$row["id"].'</td>';     echo '<td>'.$row["navn"].'</td>';     echo '<td>'.$row["indeholder"].'</td>';     echo '<td>'.$row["pris"].'</td>';     echo '<td align="center"><button type="btn btn-default" data-toggle="modal" data-target="#<?php echo $row['id']; ?>">redigere</button></td>';     echo '</tr>';                                            } 

in above, created 4 new arrays in global scope store each of values returned select query.

i changed instances of print echo. you'll want use echo in place of print when generating html mark-up. print evaluate variables pass , might see wonky results if use generate html.

now when want grab these values in second half of script, use like:

  <div class="modal-body">       er du sikker på @ du vil slette <?php echo $nav_array[0]; ?>?   </div> 

notice 0 index key referencing when using array. change match value want results.


Popular posts from this blog