-1

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Arun Kumaresh
  • 6,211
  • 6
  • 32
  • 50

7 Answers7

1

In sample.php change

$sample=Insertdata($table,$field,$data);

to

$sample=Insertdata($table,$field_values,$data_values);

that will fix most of those errors as $field and $data aren't defined in sample.php. The final error has to do with $conn which is presumably defined in connection.php

  • thank you i have changed now its throwing only one error Call to a member function query() on a non-object in /var/www/html/online_test/admin/model/insertdata.php on line 10 – Arun Kumaresh Jan 28 '16 at 06:01
  • 1
    That error is telling you that $conn isn't an object. The problem is most likely a scope issue. Right inside of the function definition at the very top add `global $conn;`. If that doesn't fix it, then it is an error within `connection.php` – Richard Theobald Jan 28 '16 at 06:04
  • 1
    Also, your `if($result)` will need to be `if($sample)` or it will always echo `not inserted` as `$result` is never defined (scope issue). – Richard Theobald Jan 28 '16 at 06:06
0

There is plenty wrong with this code, first:

$table="country";
$field_values=array("country_name","status");
$data_values=array("usa",'1');
$sample=Insertdata($table,$field,$data);

$field and $data don't exist. Make sure you are passing the correct variable names.

Also, $conn is not known within Insertdata() context, you have to either pass this variable as parameter too or make it a global.

Also, the SQL syntax is all wrong. INSERT into country country_name,statusVALUES(usa,1) is not valid SQL. Make sure you are formatting your query properly, and also that you are filtering against SQL Injection.

For example:

$fields = implode('`, `', $field);
$values = implode("', '", array_map(function ($val) use ($conn) { return $conn->real_escape_string($val); }, $data));
$sql = "INSERT INTO `{$table}` (`{$fields}`) VALUES ('{$values}')";
Havenard
  • 27,022
  • 5
  • 36
  • 62
-1

You just mis-configured your variables when calling the function.

$table="country";
$field_values=array("country_name","status");
$data_values=array("usa",'1');
$sample=Insertdata($table,$field_values,$data_values);
-1

There are so many issues in your code, first of all here is the modified code:

Modified Function:

<?
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);
}

Modified Function Calling:

$table = "country";
$field = array("country_name","status");
$data = array("usa",'1');
$result = Insertdata($table,$field,$data);
if($result)
{
    echo "inserted";
}
else
{
    echo "not inseterd";
}
?>

Issues:

  • Insertdata() function you are using VALUES as a simple string, you need to use quotes between string value otherwise it will display like this VALUES (usa,1) this should be VALUES ('usa','1') or VALUES ('usa',1).

  • You are using undefined variable in function parameters $field, $data, both of them not defined, this should be $field_values, $data_values.

  • Third, you are using undefined variable $result in success, failure condition this should be $sample not $result.

  • You are getting Fatal error because you are not using $conn in function param, limited scope

devpro
  • 16,184
  • 3
  • 27
  • 38
-2

In all of these answers you got a similar error that is 1) Notice: Undefined variable: conn 2) Warning: mysqli_query() expects parameter 1 to be mysqli, null These two errors come to avoid them try this:

function:

<?php 
function Insertdata($table,$field,$data,$conn)
{
    $field_values= implode(",",$field);
    $data_values=implode(",",$data); 

    $sql= "INSERT INTO $table (".$field_values.") VALUES (".$data_values.")";
    $result=$conn->query($sql);
}
if($result){
    echo "inserted";
} else {
    echo "not inseterd";
}
?>

function calling:

<?php 
$table = "country";
$field = array("country_name","status");
$data = array("'usa'","'1'");
$result = Insertdata($table,$field,$data,$conn);

?>

Use this code to avoid those two errors.

-2
    function InsertRecordToTable($tableName='',$fieldNames='')
    {
        $fields  = implode(',', array_keys($fieldNames));
        $values  = implode(',', mysqli_escape_string(array_values($fieldNames)));
        $insertQuery = "INSERT INTO ".$tableName.'('.$fields.') Values '.'('.$values.')';
    }

Try This Function Example with Argument Passed

$field['FirstName'] = 'Manoj';
$field['LastName'] = 'Kiran';
$field['Age'] = '21';
Insertrecord('testtable',$field);

It is working fine to insert record

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43
-2

Try This:

<?php
   $conn = mysqli_connect('localhost','root','','kailash');


   $table = "country";

   $field = array("country_name","status");
   $data = array("usa",'1');

   $field_values= implode(',',$field); 
   $data_values = implode("','",$data);

   $sql = "INSERT INTO $table($field_values)VALUES('$data_values')";
   $result = $conn->query($sql);
?>
h3t1
  • 1,126
  • 2
  • 18
  • 29
Kailash
  • 1
  • 1