Forum Moderators: coopster

Message Too Old, No Replies

mysql insert - any number of rows

         

dwighty

5:37 pm on Mar 3, 2007 (gmt 0)

10+ Year Member



Hi Guys,

i want to create a function which adds any number of fields into the required table.

i have the following code, but it returns with an error.

function db_insert($table){

$query = "INSERT INTO ".$table." (";

$trigger = 0;
foreach($_POST as $field => $value){
if($trigger > 0) $query = $query . ", ";
$query = $query . $field;
$trigger++;
}
$query = $query . ") VALUES (trim('";

$trigger = 0;
foreach($_POST as $field => $value){
if($trigger > 0) $query = $query . ", trim('";
$query = $query . $value."')";
$trigger++;
}

$query = $query . ");";

if($result = mysql_query($query)) return(0);
else echo(mysql_error());
}
//END db_insert

The way I have submitted the form is with the following:
<input type="submit" name="submit" value="Submit" title="Submit" class="formButton" />

So the error is that it returns submit is not a column in the table, which it isn't.
How do i get around this?

scriptmasterdel

6:03 pm on Mar 3, 2007 (gmt 0)

10+ Year Member



Something like:

foreach($_POST as $field => $value){
if ($field == 'submit') continue;
if($trigger > 0) $query = $query . ", ";