在SQL查询上获取错误,但正在使用正确的信息更新数据库

在SQL查询上获取错误,但正在使用正确的信息更新数据库

问题描述:

I'm getting the error: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/content/57/8912457/html/brick-academy/add_file.php on line 34.

I can't figure out why it's giving that error, considering the code looks fine and it is inputing the correct data into the correct fields in the database.

The error is stopping the rest of my script from running.

Any help would be appreciated.


Code:

include_once('resources/init.php');
include_once('user_check.php');

$name = $_POST['name'];
$teacher = $_POST['teacher'];

$query = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT name_first, name_last FROM teachers WHERE teacher_id='$teacher'");
$row = mysqli_fetch_assoc($query);
$name_first = $row['name_first'];
$name_last = $row['name_last'];
$folder_name = $name_last . "-" . $name_first . "-ID-" . $teacher . "/";


$dirname = "files/" . $folder_name;
if(!is_dir($dirname)){
    mkdir($dirname);
}

$dirname = $dirname .  $_FILES['file']['name'];


if(move_uploaded_file($_FILES['file']['tmp_name'], $dirname)){

    $query = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO documents SET id_teacher = '$teacher', name = '$name', document =  '$dirname'");
    $row = mysqli_fetch_assoc($query);

    header("location:file_manager.php");
}

我收到错误: mysqli_fetch_assoc()要求参数1为mysqli_result,布尔值在/中给出 home / content / 57/8912457 / html / brick-academy / add_file.php在第34行。 strong> p>

我无法弄清楚为什么它会给出错误,考虑到 代码看起来很好,它正在将正确的数据输入到数据库中的正确字段中。 p>

错误是停止我的其余脚本运行。 p> 任何帮助将不胜感激。 p>


代码: p>

  include_once('resources / init.php  '); 
include_once('user_check.php'); 
 
 $ name = $ _POST ['name']; 
 $ teacher = $ _POST ['teacher']; 
 
 $ query = mysqli_query(  $ GLOBALS [“___ mysqli_ston”],“SELECT name_first,name_last FROM teachers WHERE teacher_id ='$ teacher'”); 
 $ row = mysqli_fetch_assoc($ query); 
 $ name_first = $ row ['name_first']; \  n $ name_last = $ row ['name_last']; 
 $ folder_name = $ name_last。  “ - ”。  $ name_first。  “-ID-” 。  $老师。  “/”; 
 
 
 $ dirname =“files /”。  $ folder_name; 
if(!is_dir($ dirname)){
 mkdir($ dirname); 
} 
 
 $ dirname = $ dirname。  $ _FILES ['file'] ['name']; 
 
 
if(move_uploaded_file($ _ FILES ['file'] ['tmp_name'],$ dirname)){
 
 $ query = mysqli_query($  GLOBALS [“___ mysqli_ston”],“INSERT INTO文件SET id_teacher ='$ teacher',name ='$ name',document ='$ dirname'”); 
 $ row = mysqli_fetch_assoc($ query); 
 
  header(“location:file_manager.php”); 
} 
  code>  pre> 
  div>

Remove $row = mysqli_fetch_assoc($query);

If you look at the documentation for mysqli_query, it shows that it will return a boolean value for any insert queries instead of a mysql resource.
Since you're not even attempting to use $row in any place, you can get rid of the line.

change this:

$row = mysqli_fetch_assoc($query);

to this:

$row = mysqli_fetch_row($query);