I have a json format data without key value pair. when i insert data my table only the 1st data is inserted(of the array). how i insert the all data into my databse table?
<?php
$mysqli = new mysqli('localhost','root','','user');
$my_json_data = file_get_contents('./jason_data.txt');
$my_data = json_decode($my_json_data,true);
foreach($my_data as $data){
$sql = "INSERT INTO demo_table (data) VALUES ('$data');";
$mysqli->query($sql);
}
echo "done...";
?>
my database table name is 'demo_table' and json data fromat like this
["hello-world","sample-page","privacy-policy","2-revision-v1","the-banner-title"]
which is a .txt format in the same directory of my workspace
my table schema is like this
CREATE TABLE `demo_table` (
`id` int(5) NOT NULL,
`data` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Seems you have not a proper primary key.
Try create in the demo_table a proper primary key based on auto increment column
based on your schema add autoincrement
CREATE TABLE `demo_table` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`data` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
in this each row inserted have a automatic different primary key .. otherwise the first insertion create an id = 0 and the second try to do the same ..but 0 already exist
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.