Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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] => [email protected]
            )

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

    )
share|improve this question
 
what is the name of your table? –  Sankalp Mishra Feb 1 '13 at 10:22
1  
@SankalpMishra Does that matters? –  Ravi Feb 1 '13 at 10:22
 
@Ravi no, i just ran into something –  Sankalp Mishra Feb 1 '13 at 10:24
 
Hopefully the answers you get should convert it into a prepared statement rather than a basic MySQL statement –  Mark Baker Feb 1 '13 at 10:29
 
@MarkBaker : That's what I advise to do in my answer, but I'm familiar with only MySQLi which requires the type of the column to bind the values properly which the array does not provide. –  koopajah Feb 1 '13 at 10:43
add comment

4 Answers

$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);
}
share|improve this answer
 
I guess it will show error if you dont add quotes to values. –  Prasanth Bendra Feb 1 '13 at 10:25
add comment

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.

share|improve this answer
 
Why the downvote? –  koopajah Feb 1 '13 at 10:41
add comment

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;
share|improve this answer
add comment

A very simple approch would be this :

$query = 'insert into table ';
foreach($inputs as $input) {
  $query .= ' '.$input['field_name'].' = '.$input['field_value']; 
}
echo $query;
share|improve this answer
 
Why a downvote? –  Sankalp Mishra Feb 1 '13 at 10:39
 
See $query = 'insert into table where' –  Sankalp Mishra Feb 1 '13 at 10:42
 
What I mean is: there is no "where" clause in MySQL insert statement: dev.mysql.com/doc/refman/5.5/en/insert.html –  user1615903 Feb 1 '13 at 10:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.