简体   繁体   中英

convert Nested array into mysql query

I need to generate a mysql insert query with this array , where the column name are the field_name & values are field_values. i tried using implode, here its nested array & troubling me lot.

 Array
    (
        [0] => Array
            (
                [field_name] => Date
                [field_value] => 01/02/2013
            )

        [1] => Array
            (
                [field_name] => Time
                [field_value] => 03:20 PM
            )

        [2] => Array
            (
                [field_name] => submitted_lat
                [field_value] => 13.06114
            )

        [3] => Array
            (
                [field_name] => submitted_long
                [field_value] => 80.2371547
            )

        [4] => Array
            (
                [field_name] => submitted_on
                [field_value] => 2013-02-01 15:20:10
            )

        [5] => Array
            (
                [field_name] => submitted_by
                [field_value] => superadmin@gmail.com
            )

        [6] => Array
            (
                [field_name] => pdf
                [field_value] =>
            )

    )

Try this

$sql = "INSERT INTO `table_name` SET ";
$sql_fields='';

foreach($array_name as $k=>$v){
    $sql_fields .= "`".$v['field_name']."`='".$v['field_value']."', ";
}

$sql_fields = substr($sql_fields,0,-2);
$sql .= $sql_fields;
echo $sql;

A very simple approch would be this :

$query = 'insert into table ';
foreach($inputs as $input) {
  $query .= ' '.$input['field_name'].' = '.$input['field_value']; 
}
echo $query;
$fields=array();
$values=array();
for($i=0;$i<count($array);$i++)
{
   $fields[]=$array[$i]['field_name'];
   $values[]=$array[$i]['field_value'];
}

if(count($array) > 0)
{
   $query="Insert into tablename (".implode(",", $fields).") values (".implode(",", $values).")";
   mysql_query($query);
}

You need to ensure that your values are correct and protected to avoid SQL Injection. Building dynamically your own SQL query and inserting directly in your database is not safe.

Anyway you can use array_map() to do something like this:

$arr = array(array("name" => "column1", "value" => "value1"),
             array("name" => "column2", "value" => "value2"),
             array("name" => "column3", "value" => "value3"));

$columns = array_map(function($item) { return $item['name'];}, $arr);
$values = array_map(function($item) { return "'". $item['value'] . "'";}, $arr);


$columnsList = implode(',', $columns);
$valuesList = implode(',', $values);

$sql = "INSERT INTO mytable($columnsList) VALUES ($valuesList)";

EDIT: As said in introduction this would not be safe and it would be better to use MySQLi or PDO . But you would also need to know the type of the column you want to insert into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM