It is not writing in the database, and the columns have numbers as names

Asked

Viewed 41 times

1

This is a part of the code that is not writing in the database. Can help me?

 case 'questao':

   include "conect.php";
   $bd ="questoes";             
   $banco = mysqli_select_db($conexao,$bd) or (mysqli_error());

   $categoria=$_POST["categoria"];
   $enunciado=$_POST["enunciado"];
   $a=$_POST["1"];
   $b=$_POST["2"];
   $c=$_POST["3"];
   $d=$_POST["4"];
   $e=$_POST["5"];
   $resposta=$_POST["resposta"];

   mysqli_query($conexao, "INSERT INTO perguntas(1,2,3,4,5,categoria,enunciado,resposta)VALUES '$a','$b','$c','$categoria','$d','$e','$enunciado','$resposta' )");
   echo "Questoes inseridas com sucesso";

   break;   
  • Is there an error message? if you do not change the code to: mysqli_query($conexao, ' insert .........') or die(mysqli_error($conexao));

  • It appears : 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 '1,2,3,4,5,category,enunciation,response)VALUES ('Sustainability','Biodiversidad' at line 1

  • Place numbers between single quotes.

  • I tried and the same thing came up

  • 1

    I got confused, I think it’s with crase...

  • Staff freeze alternating the numbers for letters, it seems that the msql does not accept number as identifier as column but rather It starts with the remaining letters may contain number, only I am sure of that

  • And what is the purpose of this break?

  • ah part of the code I’m implying bang using swtich;

Show 3 more comments

1 answer

2


It is NOT advisable to use numbers as identifiers of tables or columns but it is possible to use them as long as the names are escaped with backticks (crase), this is also for special characters, accents and reserved words like DESC for example.

Example - sqfiddle

CREATE  TABLE `10` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `1` VARCHAR(45) NULL ,
  `2` VARCHAR(45) NULL ,
  PRIMARY KEY (`id`) );

correct Insert:

insert into `10` (`1`, `2`) values ('a', 'b')

wrong Insert:

insert into 10 (1, 2) values ('a', 'b')

Browser other questions tagged

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