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

时间:2019-03-08 01:34:50

标签: javascript php jquery ajax codeigniter

我正在使用AJAX来创建动态相关的选择框,但无法使用户将不同的数据添加到同一表中。

代码:

<?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') ");
}
?>

我正在使用事务或临时文件,但不起作用。

1 个答案:

答案 0 :(得分:2)

尝试使用我的代码:

<?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.

 ?>
  

注意:为什么我建议使用get row,这是因为我看到您只需要1个数据,要使用result并调用数据库中的任何值,这会使您的服务器运行缓慢(如果您的网站流量稳定) )