简体   繁体   中英

insert json_encode values into mysql table

I am working in PHP.I have the following values which is done the json_encode function.When i print the variable $vmndetails i got the below details.

[{
   "id":"1",
   "smsid":"4781366",
   "senderid":"289613638",
   "textcontent":"Good day reply",
   "msgdate":"2014-12-17 13:04:20",
   "charsetval":"UTF-8",
   "userid":"",
   "reprtdate":"2014-12-17 11:04:21"
},   
{
   "id":"7",
   "smsid":"4781467",
   "senderid":"289761363",
   "textcontent":"Good",
   "msgdate":"2014-12-17 13:21:18",
   "charsetval":"UTF-8",
   "userid":"",
   "reprtdate":"2014-12-17 11:21:21"
}]

I have to insert these values into my mysql table.

So i wrote the below function.

mysql->query( "INSERT INTO twowaysms(id,smsid,senderid,textcontent,msgdate,charsetval,userid,reprtdate) VALUES "."(" . implode(",",$vmndetails) . ")");

But i am getting an error that 'Insert value list does not match column list: 1136 Column count doesn't match value count at row 1'.I have seen the same question a lot here but i cannot find a solution.How to solve the issue?Please anyone help me..

You have mulit-dimensional array data, so try like this -

// http://ideone.com/xX2HNU
$vmndetails = '[{"id":"1","smsid":"4781366","senderid":"289613638","textcontent":"Good day reply","msgdate":"2014-12-17 13:04:20","charsetval":"UTF-8","userid":"","reprtdate":"2014-12-17 11:04:21"},{"id":"7","smsid":"4781467","senderid":"289761363","textcontent":"Good","msgdate":"2014-12-17 13:21:18","charsetval":"UTF-8","userid":"","reprtdate":"2014-12-17 11:21:21"}]';

$vmndetails = json_decode($vmndetails, true);
$values = "";
foreach($vmndetails as $v) {
    $values .= "(".implode(", ", $v)."), "
}
$values = rtrim($values, ", ");

mysql->query( "INSERT INTO 
    wowaysms(id,smsid,senderid,textcontent,msgdate,charsetval,userid,reprtdate) 
    VALUES {$values} ");

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