View Full Version : How to String SQL statement in PHP with array variable
metomeya
02-25-2010, 06:53 PM
I'm getting an error:
Parse error: syntax error, unexpected '"', expecting ']' at the line that says '$_POST[ingredientAMT" . $i . "]',
I have the following code:
for ($i=1; $i<=$_POST[ingre_amt]; $i++)
{
$SQL1 = "INSERT INTO
recipeingredients (MealID, IngreAmt, UnitID, IngreID)
VALUES (
'$_POST[mealID]',
'$_POST[ingredientAMT" . $i . "]',
'$_POST[ingredientUNIT" . $i . "]',
'$_POST[ingredientNAME" . $i . "]')";
}
tomws
02-25-2010, 07:26 PM
It's SQL, not SLQ. And it's a PHP problem, not an SQL issue.
You can't use $_POST['whatever'] inside a string like that. Two solutions: 1) use concatenation like you did with the $i variable, or 2) wrap it in curly braces like {$_POST['whatever']}.
metomeya
02-25-2010, 07:36 PM
I changed the code to the following and I'm still getting the same error...
for ($i=1; $i<=$_POST[ingre_amt]; $i++)
{
$SQL1 = "INSERT INTO
recipeingredients (MealID, IngreAmt, UnitID, IngreID)
VALUES (
{$_POST[mealID]},
{$_POST[ingredientAMT" . $i . "]},
{$_POST[ingredientUNIT" . $i . "]},
{$_POST[ingredientNAME" . $i . "]})";
}
For concatenation I tried this and I'm still getting the same error
for ($i=1; $i<=$_POST[ingre_amt]; $i++)
{
$SQL1 = "INSERT INTO
recipeingredients (MealID, IngreAmt, UnitID, IngreID)
VALUES (
'$_POST[mealID]',
'$_POST[ingredientAMT";
$SQL1 .= $i;
$SQL1 .= "]', '$_POST[ingredientUNIT";
$SQL1 .= $i;
$SQL1 .= "]', '$_POST[ingredientNAME";
$SQL1 .= $i;
$SQL1 .= "]')";
}
Old Pedant
02-25-2010, 07:36 PM
Believe it or not, you don't have enough quote marks in there.
To get the value of a POST element, you must do
$_POST["name"]
So if you want the i'th ingredientAMT, you would need
$_POST["ingredientAMT" . $i . ]
And while you *could* munge that ugly string to get all the needed quote marks in place, I strongly advise that you *NOT* do so. It makes debugging nearly impossible.
Instead, do it this way:
for ($i=1; $i<=$_POST[ingre_amt]; $i++)
{
$id = $_POST["mealID"];
$amt = $_POST["ingredientAMT" . $i];
$unit = $_POST["ingredientUNIT" . $i];
$name = $_POST["ingredientNAME" . $i];
$SQL1 = "INSERT INTO recipeingredients (MealID, IngreAmt, UnitID, IngreID) "
. " VALUES ('$id','$amt','$unit','$name');" ;
... don't forget to execute that sql here ...
}
tomws
02-25-2010, 07:42 PM
To get the value of a POST element, you must do
$_POST["name"]
Unfortunately, that's incorrect. Using unquoted array keys, while wrong, still works in PHP. From the PHP manual (http://php.net/manual/en/language.types.array.php):
Why is $foo[bar] wrong?
Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why? It is common to encounter this kind of syntax in old scripts:
PHP Code:
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.
Old Pedant
02-25-2010, 08:34 PM
Huh...PHP is just plain weird.
Even so, I stand by my answer. It's by far the best and simplest way to do this and provides the best opportunity for debugging when something goes wrong. Why write obscure code when nice clean simple code works so well?
metomeya
02-25-2010, 08:40 PM
You the man Old Pedant!!!
(little does he know he has written almost half my code through all my question ;) )
Thank you!
Old Pedant
02-25-2010, 09:30 PM
One of these days maybe I'll even learn PHP. But not soon. Too busy with ASP/ASP.NET/JSP for now.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.