Loop syntax when it has no results


I have a loop that runs 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 enter a condition if there is no input that does not execute that loop but it gives me syntax error, so I had thought that if there is no select put a variable that says that the value is 0 so that the loop is only run once.

But when I make disappear the select 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 makes the loop

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 within the $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 is before the error is just the variable of $ TallaLin [$ i].

On the other hand, if $ _SESSION ['Company_Tallas'] is equal to 1, it does not give the syntax error.

What can I be doing wrong?

asked by Killpe 30.01.2018 в 22:49

1 answer


Your problem is that when you assign the value to $TallaLin , you are assigning it as an integer, 0, when you do not have value in $_SESSION['Empresa_Tallas'] .

Actually it should be a string, so your code should be like this:

if ($_SESSION['Empresa_Tallas'] == 1) { 
    $TallaLin = $_POST["Cosa_MultiTallas"]; 
} else { 
    $TallaLin = "0"; //El 0 tiene que ir entre comillas para considerarlo como String
answered by 30.01.2018 / 23:02