简体   繁体   English

如何使用AJAX插入具有不同连接ID的同一表

[英]How to insert same table with different connection ID with AJAX

I'm using AJAX to make dynamic dependent select box, but I can't make users add different data to the same table. 我正在使用AJAX来创建动态相关的选择框,但无法使用户将不同的数据添加到同一表中。

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 ) 注意:为什么我建议使用get row,这是因为我看到您只需要1个数据,要使用result并调用数据库中的任何值,这会使您的服务器运行缓慢(如果您的网站有solidtraffic)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM