How to insert data to table1 with checking field f

2019-04-15 05:59发布

问题:

Here is table1's structure:

  1. id_data_inserted
  2. date
  3. id_data
  4. id_room

table2's structure:

  1. id_data
  2. data_name
  3. specification
  4. amount
  5. price

I'm trying to insert data into table1, but before inserting data to table1, I need to check the id_data table1 and table2. If id_data from table2 same with id_data table1 data will be inserted to the table, and if not, data will not inserted to the table.

For Example:

  • Data from table2

  • Simple Form screenshot

I already have a simple form for input data to the database. in table2 there is a data that has id_data = AA001, and if we insert data to table1 with a value of id_data from the simple form(if we insert AA001 in the form), it will be inserted, but when we input id_data which are not AA001 it will fail. (not inserted to the database).

And now what should I do?

EDIT

This is my model:

<?php
class My_Model extends CI_Model
{

    public function __construct()
    {
       parent::__construct();
    }

    public function input_data($data, $table)
    {
       $this->db->insert($table,$data);
    }

    public function view_data($table)
    {
       return $this->db->get($table);
    }

    public function checkIdData()
    {
       $query = $this->db->query("SELECT id_data FROM table2");
       return $query;
    }
}

And this is input_data_inserted function in my Controller:

public function input_data_inserted()
{
    $id_data_inserted = $this->input->post('id_data_inserted');
    $date = $this->input->post('date');
    $id_data = $this->input->post('id_data');
    $id_room = $this->input->post('id_room');

    $checkID_Data = $this->my_model->checkIdData()->result_array();

    if($id_data == $checkID_Data)
    {
        $data = array
        (
           'id_data_inserted' => $id_data_inserted,
           'date' => $date,
           'id_data' => $id_data,
           'id_room' => $id_room
        );

        $this->my_model->input_data($data, 'table1');
        $data['table1'] = $this->my_model->viewl_data('table1')->result();
        $this->load->view('admin/data/v_data', $data);
    }
    else
    {
       echo "Failed to input!";
    }
}

I'm trying this code but if i insert id_data = AA001 in the form, the execution always go to the else statement. But if i change the value of $checkID_Data with string ( now like this: $checkID_Data = "AA001" ) the execution will go to the if statement and data succesfully inserted.

Any Solution?

回答1:

You can write an insert select statement.

This is just a rough visualization since you haven't provided your code. Let me know if you need clarification.

INSERT INTO table1 (id_data_inserted, date, id_data, id_room) 
    SELECT
        '$id_data_inserted' as id_data_inserted,
        '$date' as date,
        '$id_data' as id_data,
        '$id_room' as id_room
    FROM table2 WHERE id_data = $id_data

Answer on your update:

Here's my updated answer for your controller. But I still prefer my first answer because that's much faster.

$checkID_Data = $this->my_model->checkIdData()->result_array();
$passed = FALSE;

foreach ($checkID_Data as $v)
{
    if($id_data == $v['id_data '])
    {
        $data = array
        (
           'id_data_inserted' => $v['id_data'],
           'date' => $date,
           'id_data' => $id_data,
           'id_room' => $id_room
        );

        $this->my_model->input_data($data, 'table1');
        $data['table1'] = $this->my_model->viewl_data('table1')->result();
        $this->load->view('admin/data/v_data', $data);
        $passed = TRUE;
        break;
    }
}

if (!$passed)
{
   echo "Failed to input!";
}