Jump to content

[SOLVED] Using $_POST


ksmatthews

Recommended Posts

Hi all,

 

I need to create some huge forms. When it comes to the backend submission, the SQL insert becomes large

and cumbersome.

 

Is there an easier way to insert a large amount of POSTED data without having to explicitely refer to each

and every form variable by name when creating the SQL insert string ?

 

I have tried code like this

 

$values = serialize($_POST);

$SQL = 'insert into mytable values (' .$values. ')';

 

but that does not work ...

 

How can I access each and every form field in $_POST without referring to names ?

 

regards,

 

Steven M

 

:(

Link to comment
Share on other sites

An easy way to do what you're asking would be to just loop through the $_POST array provided that each item in the $_POST array corresponds to a field in your DB.  This is how I would approach it:

 

$field_sql = "";
$value_sql = "";

foreach($_POST AS $name => $value)
{
$field_sql .= $name . ",";
$value_sql .= "'" . $value . "',";
}

$sql = "INSERT INTO table (" . rtrim($field_sql, ",") . ") VALUES (" . rtrim($value_sql, ",") . ")";
mysql_query($sql);

 

Now this is a bit general, you'd want to do any kind of input filtering within the foreach loop itself such as addslashes, etc.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.