I have a loop that goes through an array that comes from a multiple select and stores the results in mysql.
The fact is that there is the option that this select does not exist, so the loop does not make sense. I have tried to insert a condition of if the input does not exist that does not execute that loop but it gives me a syntax error, so I had thought that if the select does not exist, insert a variable that says that the value is 0 so that the loop can only be run once.
But when I make the select disappear it gives syntax error when saving in mysql :(
This is the code I'm using:
if ($_SESSION['Empresa_Tallas'] == 1) { $TallaLin = $_POST["Cosa_MultiTallas"]; } else { $TallaLin = 0; }
Here I define if I want to use sizes or not and if I use sizes I read the select " Cosa_Multitallas
" and if not $TallaLin is 0.
And then I have the for that loops
for ($i=0;$i<count($TallaLin);$i++) { //hago un bucle que recorre todas las tallas
//Guardado a mysql
}
and as a variable for size mysql I use this syntax inside the for$TallaLin[$i]
The error that mysql gives me is this:
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 ''0','','','','22','0','5','')' at line 1
what there is before the error is just the variable of $TallaLin[$i].
On the other hand, if $_SESSION['Company_Sizes'] is equal to 1, it does not give the syntax error.
What I can be doing wrong?
Your problem is that by assigning the value to
$TallaLin
, you are assigning it as an integer, 0, when you have no value in$_SESSION['Empresa_Tallas']
.It should actually be a string, so your code should look like this: