PHP syntax error when inserting an image

Asked

Viewed 62 times

-1

I’m inserting an image into the database with the following code

$nome_img = $_FILES['imagem']['name'];
if(move_uploaded_file($_FILES['imagem']['tmp_name'], "images/Produtos/".$nome_img){
   $query=  "INSERT INTO produtos(ImagemProduto) VALUES ('$nome_img'))";
}else{
   echo "Erro!";
}

And you’re making the following mistake:

Parse error: syntax error, Unexpected ';' in C: xampp htdocs Site user functions.php on line 218

Line 218 is the query line

$query=  "INSERT INTO produtos(ImagemProduto) VALUES ('$nome_img')";

2 answers

0

So apparently it’s correct, but to make a mistake, it’s probably related to " and the '.

I’d try rewriting the line, without copying/pasting, you might even try using a different form of string Interpolation, thus

$query = "INSERT INTO produtos(ImagemProduto) VALUES ('" . $nome_img . "')";

or

$query = "INSERT INTO produtos(ImagemProduto) VALUES ('{$nome_img}')";

EDIT

The problem is actually the lack of ) of if just above the line where the variable $query is defined.

Plus, you also need to add the instruction to run the query in the database, right on the next line where you are setting the variable $query.

$result = mysqli_query($db, $query);

  • even writing both lines of code by hand gives error

  • The problem is in if. Missing one ) before opening the key {. if(move_uploaded_file($_FILES['imagem']['tmp_name'], "images/Produtos/".$nome_img) ) {

  • that solved the error , now is not registering

  • You are returning "Error!"?

  • You need to run the SQL query in the database. You can use mysqli_query($query); right after you set the variable $query. https://www.w3schools.com/php/func_mysqli_query.asp http://php.net/manual/en/mysqli.query.php

  • Hello friend. first of all I fixed it here: products(Imagemproduto). Separates products from parenthesis products (Imagemproduto). See if it helps, if not maybe in the matter of mysqli Insert.

  • @Dávilandré was not that still wrong when inserting the image

  • @Joségomes changed the answer to give some more information. I think the problem you have now is that you are not connected to a database. I put some links for documentation that should help you.

  • @Joségomes the problem now is the destination path of move_uploaded_file incorrect or the folder does not have the right permissions.

  • the way is right

Show 5 more comments

0

Your error is in the upload line:

if(move_uploaded_file($_FILES['imagem']['tmp_name'], "images/Produtos/".$nome_img){

There’s one missing ), should be:

if(move_uploaded_file($_FILES['imagem']['tmp_name'], "images/Produtos/".$nome_img)){
  • Era. Query currently missing :)

  • @Milk your question has no query execution code, no PDO has no mysqli, it is impossible to help without knowing what the code is like, only has a string in a variable inside the if can show the code I can help.

  • that parenthesis has already been placed and continues to give the message "Error!". or is not uploading the image

  • It’s not my question. I was just pointing out that the problem is already another.

  • 1

    @Milk the title of the question and the description of the question only speak of syntax error, this answered to what was asked, was not informed anywhere of the question difficulty about the execution of Apis to access database, the question is about syntax error, It’s hard to respond to something that has no code to know where it failed.

  • @Guilhermenascimento yes, I agree.

  • @Joségomes then can be 3 errors, 1. or the folder images/Produtos/ no 2. the folder images/Produtos/ is inaccessible, 3. is using linux server and so the files and folders have case-sensitive names, and you have entered the wrong name.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.