i want to make a function that have two parameters which are table name and $info(includes column name and its values)
for example
$info=array('em_name'=>'Ali',
'em_salary'=>'3000',
'em_city'=>'ISB',
'em_country'=>'Pakistan');
i want to call function.
insert($info,emp);
that give me the result as follow
insert into emp (emp_name,emp_salary,emp_city,emp_country) value("Ali",'3000','ISB', "Pakistan");
any one help me for makeing this function??
for field name use array_keys and implode , for value use array_values and implode
function insert($value,$table){
$qry = 'insert into '. $table .'('.implode(",",array_keys($value)).') values("'.implode('", "', array_values($value)).'")';
echo $qry;
}
$info=array('em_name'=>'Ali',
'em_salary'=>'3000',
'em_city'=>'ISB',
'em_country'=>'Pakistan');
insert($info,'emp');
Try This:
$info=array(
'em_name'=>'Ali',
'em_salary'=>'3000',
'em_city'=>'ISB',
'em_country'=>'Pakistan'
);
function insert($info, $table_name){
$sql = 'insert into '. $table_name .' ('.implode(',',array_keys($info)).') values("'.implode('", "', array_values($info)).'")';
}
insert($info, 'emp');