Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. Invalid argument supplied for foreach() in sqlinfo.php on line 28
  2.  
  3. if($row = $result->fetch_assoc() && $result->num_rows === 1){
  4. foreach($row as $key => $value){ //error
  5.  
  6. $(document).ready(function () {
  7. $(".grid").on("click", ".edit", function (){
  8. var albumId = $(this).siblings(".grid-info").attr("id");
  9. var imageId = $(this).siblings("img").attr("id");
  10. var request = (albumId == '') ? {requestType: 'Album', id: albumId} : {requestType: 'Image', id: imageId};
  11. var getSQLInfo = $.ajax({
  12. url: '../P3_M2/ajax/sqlinfo.php',
  13. method: 'post',
  14. data: request,
  15. dataType: 'json',
  16. error: function(error) {
  17. console.log(error);
  18. }
  19. });
  20.  
  21. });
  22.  
  23. <?php
  24. require_once('../configsql.php');
  25.  
  26. $queryFor = array(
  27. 'Album' => 'SELECT * FROM Album WHERE id = ?',
  28. 'Image' => 'SELECT * FROM Image WHERE id = ?');
  29.  
  30. $requestType = filter_input(INPUT_POST, 'requestType', FILTER_SANITIZE_STRING);
  31. if (empty($requestType)) {
  32. echo 'Missing requestType.';
  33. die();
  34. }
  35.  
  36. $id = $mysqli->real_escape_string($_POST["id"]);
  37.  
  38. $info= array();
  39. $stmt = $mysqli->prepare($queryFor[$requestType]);
  40. $stmt->bind_param('i', $id);
  41. $executed = $stmt->execute();
  42. if(!$executed){
  43. echo "SQL query $querFor[$requestType] not executed";
  44. exit();
  45. }
  46. $result = $stmt->get_result();
  47. $stmt->close();
  48. if($row = $result->fetch_assoc() && $result->num_rows === 1){
  49. foreach($row as $key => $value){ //error here
  50. //Transform SQL field names to form input names for html
  51. $key = str_replace('_', '-', $key);
  52. if($key === 'title'){
  53. $key = strtolower($requestType).'-'.$key;
  54. }
  55. $info[$key] = $value;
  56. }
  57. }
  58.  
  59. // Send back the array as json
  60. echo json_encode($info);
  61.  
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement