simple sql insert function in php

php function :
<?php
function insertSQL($post, $exvar, $table)
{
$x=0;
$fld=array();
$vlu=array();
foreach($post as $key=>$val){
if( !in_array($key, $exvar) ){
$fld[$x]=$key;
$vlu[$x]=”‘”.$val.”‘”;
++$x;
}
}
$sql = “insert into $table (“.implode(“,”,$fld).”) values (“.implode(“,”,$vlu).”)”;
return $sql;
}
if($submit) {
$ex = array(‘submit’=>’submit’,’reset’=>’reset’);
echo insertSQL($data,$ex, ‘tableUser’);
}
?>

html :

<form method=”post”>

<input type=”text” name=”name”>

<input type=”text” name=”middlename”>

<input type=”submit” name=”submit” value=”submit”>

</form>

p/s: no input validation… html form, the name of the input must be the same with the name of the column/field in tables.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s