Reply to Re: $_POST and Building SQL Strings

Your name:

Reply:


Posted by David Gillen on 05/04/07 11:17

john said:
> All:
>
> I'm a long-time developer, new to PHP....
>
> Is there an idiom used in PHP to construct SQL statments from $_POST
> data?
>
> I would guess that in many applications, the data read from $_POST are
> used to build SQL statements. Certainly, we can do the following:
>
> $email = $_POST['email']
> $sql = "insert ... values ('$email')..."
>
$sql = "INSERT .. values ('{$_POST['email']}')";

The braces around the variable will result in it being substituted correctly
for it's value. Not the most readable unfortunately.

But also if you have lots of post variables and don't want to do
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
etc
You can insert the following snippet
foreach($_POST as $key=>$val)
{
$$key = $val;
}
or single line
foreach($_POST as $key=>$val){$$key = $val;}

And now you can reference all the variable as $var1, $var2 etc. The $$ is
variable variable notation. So do that and then
$sql = "INSERT ... values ('$var1','$var2',...etc)";
Much more readable.

Hope that helps.

D.
--
Fermat was right.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация