I'm using AJAX to make dynamic dependent select box, but I can't make users add different data to the same table.
Code:
<?php
if (isset($_POST['value'])) {
$size = $_POST['value'];
$query = $this->db->query("SELECT * FROM price_poster WHERE size = '$size' LIMIT 1");
$quantity = $this->db->query("SELECT * FROM number_price_poster ORDER BY id DESC LIMIT 1");
$res = $quantity->result();
$row = $res[0];
$quantity_id = $row->no_quantity;
foreach ($query->result_array() as $data) {
$hasil = ($quantity_id * $data["price"]);
echo '<input type="text" name="priceposter" class="form-control" value="Rp.'.number_format($hasil,2,',','.').'" readonly="true"></input>';
}
$queryz = $this->db->query("INSERT INTO number_price_poster VALUES ('', '$size', '$quantity_id') ");
}
if (isset($_POST['value1'])) {
$quantity = $_POST['value1'];
$size = $this->db->query("SELECT * FROM number_price_poster ORDER BY id DESC LIMIT 1");
$res = $size->result();
$row = $res[0];
$size_id = $row->no_size;
$query = $this->db->query("SELECT * FROM price_poster WHERE size = '$size_id' LIMIT 1");
foreach ($query->result_array() as $data) {
$hasil = ($quantity * $data["price"]);
echo '<input type="text" name="priceposter" class="form-control" value="Rp.'.number_format($hasil,2,',','.').'" readonly="true"></input>';
}
$queryz = $this->db->query("INSERT INTO number_price_poster VALUES ('', '$size_id', '$quantity') ");
}
?>
I'm using transaction or temp, but it's not working.
Try use my code :
<?php
$pos = $this->input->post();
// reduce error using this method.
if(isset($pos['value']))$value = $pos['value'];else $value='';
if(isset($pos['value']))$value1 = $pos['value1'];else $value1='';
if($value)
{
// for the security reason i recommend you using query builder
$price = $this->db->select('price as p')
->from('price_poster')
->where('size',$value)
->limit(1)
->row();
$quantity_id = $this->db->select('no_quantity as nq')
->from('number_price_poster')
->order('id','desc')
->limit(1)
->row() // Recommend using row for getting 1 data;
$hasil = $quantity_id->nq * $price->p;
$data['input_form'] = '<input type="text" name="priceposter" class="form-control" value="Rp.'.number_format($hasil,2,',','.').'" readonly="true"></input>'; // i recommended dont use echo for final production , for send data i recommend using this methond to send data into your view.
// next imput data into number_price_poster
$datanya=array(
'price'=>$value,
'no_quantity'=>$quantity_id->nq
);
$this->db->insert('number_price_poster',$datanya);
$res_id = $this->db->insert_id(); // get check insert is success
if(!$res_id)echo "upsss something error";
}
$this->load->view('view',$data); // i recommend dont - print value and send it to your view and process it.
?>
Note : why i recommend using getting row , it's because i see you just need 1 data, to using result and call any value in data base it's make your server slowly (if your website have solidtraffic )
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.