Syntax error in SELECT MAX

Asked

Viewed 114 times

1

I have a syntax problem in my sql code inside the php code, I need to know which video has the biggest id in the database to save it in a separate folder with the same id number, so that the path is saved in the database. The mistake that happens is: You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near ' deo)FROM video' at line 1

<?php 
include "Conection.php";
include "select.php";
$nome = $_POST['nome_video'];
$genero = $_POST['genero'];
$comando = "SELECT MAX(id_vídeo)FROM video;";
$select = mysqli_query($con,$comando);
if (!$select){
printf("Error: %s\n", mysqli_error($con));
exit();
}
$dado = mysqli_fetch_array($select); 
$max_id = $dado['id_vídeo'];
$new_id = $max_id + 1;
$a = "C:/Users/ian/Desktop/UploadTeste/".$new_id;
print $a;
$query = "INSERT INTO video (CPF,nome_vídeo,genero,caminho) VALUES ('$cpf_cookie','$nome','$genero','$a');";
$insert = mysqli_query($con,$query);
mysqli_close($con);
if($insert== true){
    echo"<script language='javascript' type='text/javascript'>alert('upload realizado com sucesso!');window.location.href='MinhasProducoes.php'</script>";
}
else{
    echo"<script language='javascript' type='text/javascript'>alert('Não foi possível fazer o upload desse vídeo');window.location.href='##EnviarProdução.php'</script>";
}
$destino = 'C:/Users/Ian/Desktop/UploadTeste/' . $_FILES['arquivo']['name'];

$arquivo_tmp = $_FILES['arquivo']['$new_id'];

move_uploaded_file( $arquivo_tmp, $destino  );
  • From a look in the parentheses are glued in the from.

  • $command = "SELECT MAX(id_video)FROM video;"; Well, you don’t need ";" inside select, and video does have an accent? (I think this might be the error).

  • Yes, it does have an accent and I can not give an alter table, because it is a primary key :(

1 answer

1


The problem is the encoding of the accent in id_video:

SELECT MAX(id_vídeo)FROM video;

Most likely your file is with a different encoding than mysql client expects

Example per command line:

SELECT MAX(id_vídeo) FROM video;
+------------+
| MAX(vídeo) |
+------------+
|       NULL |
+------------+

Now the same example, only the character copied from your question (simulating an invalid character:

SELECT MAX(id_v�deo)FROM video;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?deo)FROM a' at line 1

(Note that the error is the same, showing the exact position of the invalid character, it is just an example, I may have used different encodings here that result in the same problem)

To correct:

  • See what encoding your file is in, use mysqli_set_charset to make your mysql client (php/Pdo) use the same encoding as your file
  • Or save your file in the same mysql client encoding (php/Pdo)

Suggestions:

Browser other questions tagged

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