I'm trying to write a PHP function Insertdata($table,$field,$data)
that will insert some data. Here is what I've got so far:
insertdata.php
<?php
include('connection.php');
function Insertdata($table,$field,$data){
$field_values= implode(',',$field);
$data_values=implode(',',$data);
$sql="INSERT into". " ".$table." ".$field_values. "VALUES(".$data_values.")";
$result=$conn->query($sql);
}
?>
The call of this function happens in another page:
sample.php
<?php
include('../model/insertdata.php');
$table="country";
$field_values=array("country_name","status");
$data_values=array("usa",'1');
$sample=Insertdata($table,$field,$data);
if($result)
{
echo "inserted";
}
else
{
echo "not inseterd";
}
?>
Whenever I call the function above the following error gets thrown and no data is inserted:
Notice: Undefined variable: field in /var/www/html/online_test/admin/view/sample.php on line 6
Notice: Undefined variable: data in /var/www/html/online_test/admin/view/sample.php on line 6
Warning: implode(): Invalid arguments passed in /var/www/html/online_test/admin/model/insertdata.php on line 6
Warning: implode(): Invalid arguments passed in /var/www/html/online_test/admin/model/insertdata.php on line 7
INSERT into country VALUES()
Notice: Undefined variable: conn in /var/www/html/online_test/admin/model/insertdata.php on line 10
Fatal error: Call to a member function query() on a non-object in /var/www/html/online_test/admin/model/insertdata.php on line 10
I'm new to PHP and in the process of learning. I need some advice how to get this working.