I want insert this value JSON to database, but with following PHP code it insert just [
in table. how can fix this problem?
Html code:
<input name="start_date[]" value="2012/08/07">
<input name="start_date[]" value="2011/10/29">
<input name="percent[]" value="56">
This value JSON: ["2012\/08\/07","2011\/10\/29"]
PHP code:
$start_date = json_encode($this->input->post('start_date'));
$percent = $this->input->post('percent');
print_r($start_date); // This output is as: ["2012\/08\/07","2011\/10\/29"]
$data = array();
foreach ($percent as $idx => $name) {
$data[] = array(
'start_date' => $start_date[$idx],
'price_change' => $percent[$idx]
);
};
$this->db->insert_batch('table', $data);
Actually I don't understand what you trying to achieve but I give it a go. Does this help if you just remove "[$idx]" from your $start_date. So record to database would look like:
$data[] = array(
'start_date' => $start_date,
'price_change' => $percent[$idx]
);
The "
may be confusing it. I can't tell what kind of database system you're using, but maybe you should try escaping the "
to \"
(with the help of addslashes()
) and see if that fixes anything.