Syntax error in SELECT MAX

1

I have a syntax problem in my sql code inside the php code, I need to know which video has the largest id in the database to save it to a separate folder with the same id number, so that the path gets saved in the bank. The error 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  );
    
asked by anonymous 01.07.2018 / 21:35

1 answer

1

The problem is encoding the accent in id_video :

SELECT MAX(id_vídeo)FROM video;

Most likely your file will have a different encoding than the mysql client expects

Example by 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, just an example, I may have used different encodings here that result in the same problem)

To fix:

  • 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 code (php / pdo)

Suggestions :

  • Instead of quarreling with accents in mysql (even if MySQL supports it), use columns with a-z and _ names, so you do not care about different encodings.
  • If you have problems changing primary keys: link link
  • Separate SELECT columns with space before FROM , MySQL only understands separating to Tokens in Query because of parentheses in MAX(id_vídeo)FROM
02.07.2018 / 04:31