Re: Smarter way to extract data from FORM and save to DB?



On Fri, 08 Feb 2008 09:57:04 +0100, Gilles Ganault <nospam@xxxxxxxxxx> wrote:

Hello

Out of curiosity, is there a smarter, easier way to read data sent by
a form, and save them into a database? I have about 20 fields, and
it'd be easier if I could just use a loop to go through an array and
generate the SQL query in a couple of lines:

======
//If 'id' set -> update; Otherwise -> insert
if($_POST['id'])
$sql = sprintf("UPDATE $table SET name='%s', tel='%s' WHERE
id=%s",$_POST['name'],$_POST['tel'],$_POST['id']);
else
$sql = sprintf("INSERT INTO $table (id,name,tel) VALUES
(NULL,'%s','%s')",$_POST['name'],$_POST['tel']);
======

A safer way would be prepared statements. This code is, seeing to the use of sprintf(), very easily altered to use those.

--
Rik Wasmus
.



Relevant Pages