Problem with executing sql query, here is the code that returns an error

Warning: mysqli_error() expects parameter 1 to be mysqli, string given in E:\OpenServer\domains\demo\templates\admin\uploadGallery.php on line 43 

I do not understand what the error is, because the first parameter of mysqli and Google did not find a solution.

 <?php $connect = mysqli_connect("localhost", "root", "", "cms"); $sql = "INSERT INTO images_path ( name, pathTo, type) VALUES ($name,$pathTo,$type)"; $pathTo = "post-images/gallery/"; $name = $_FILES['filename']['name']; $type = $_FILES['filename']['type']; $types = array('image/gif', 'image/png', 'image/jpeg', 'image/pjpeg','image/jpg' ); if($_FILES["filename"]["size"] > 1024*3*1024 && !in_array($_FILES['file']['type'], $types)) { echo ("Размер файла превышает три мегабайта или недопустимый формат"); exit; } if(is_uploaded_file($_FILES["filename"]["tmp_name"])) { move_uploaded_file($_FILES["filename"]["tmp_name"],"$pathTo".$_FILES["filename"]["name"]); mysqli_query($connect,$sql) or mysqli_error($sql); echo("Ееее роцк!!"); } else { echo("Ошибка загрузки файла"); } ?> 
  • one
    the string c $ sql must be placed below the variable declaration - G.Denis
  • it doesn't help anyway, I’ve played it as I wish - vovaxxx
  • And which line is wrong? And then, why don't you check if mysqli has successfully connected? It’s necessary to check if $ connect is null at least .. Or put a connection in try-catch ... - Michael Vaysman
  • one
    INSERT INTO images_path (`name`,` pathTo`, `type`) VALUES ('$ name', '$ pathTo', '$ type') - G.Denis

1 answer 1

First, as @ G.Denis indicated, "the string c $ sql needs to be placed below the variable declaration", otherwise you have $sql === 'INSERT INTO images_path ( name, pathTo, type) VALUES (,, )'; - obviously not what you expected.
Also, variables inside $ sql must be placed in single quotes.

Secondly, mysqli_error($sql); - you pass $sql to the function, that is, a string, but you need to pass $connect .

  • 2
    About mysqli_error, more variables inside $sql must be placed in single quotes - G. Denis
  • Thank you, helping out a second time) next time I will try to be more attentive - vovaxxx