-1

The problem is when I entered a new name no data is added. A similar thing happen when I entered an already existing name. Still, no data is added to the database. I am still new to CodeIgniter and not entirely sure my query builder inside the model is correct or not.

In the Model, I check if the name already exists insert data only into the phone_info table. IF name does not exist I insert data into user_info and phone_info.

Controller:

public function addData()
{
    $name = $this->input->post('name');
    $contact_num = $this->input->post('contact_num');
    if($name == '') {
        $result['message'] = "Please enter contact name";
    } elseif($contact_num == '') {
        $result['message'] = "Please enter contact number";
    } else {
        $result['message'] = "";
        $data = array(
            'name' => $name,
            'contact_num' => $contact_num
        );
        $this->m->addData($data);
    }
    echo json_encode($result);
}

Model:

public function addData($data)
{
    if(mysqli_num_rows($data['name']) > 0) {
        $user = $this->db->get_where('user_info', array('name' => $data['name']))->result_array();
        $user_id = $user['id'];
        $phone_info = array(
            'contact_num' => $data['contact_num'],
            'user_id' => $user_id
        );
        $this->db->insert('phone_info',$phone_info);
    } else {
        $user_info = array(
            'name' => $data['name']
        );
        $this->db->insert('user_info', $user_info);
        $user = $this->db->get_where('user_info', array('name' => $data['name']))->result_array();
        $user_id = $user['id'];
        $phone_info = array(
            'contact_num' => $data['contact_num'],
            'user_id' => $user_id
        );
        $this->db->insert('phone_info', $phone_info);
    }
}

DB-Table user_info: user_info table

DB-Table phone_info: phone_info table

Jonathan
  • 1,955
  • 5
  • 30
  • 50
  • Please do not post screenshots of your code. Post actual code. Refer to [Formatting tips](https://stackoverflow.com/editing-help) – Jonathan Jul 26 '21 at 10:27
  • The code looks almost correct. But I am not sure if `mysqli_num_rows($data['name']) > 0` works as you'd expect. Can you asure that `$this->m->addData($data);` is called? – Jonathan Jul 26 '21 at 10:40

1 Answers1

0

Extend and change your model to this:

public function findByTitle($name)
{
    $this->db->where('name', $name);
    return $this->result();
}

public function addData($data)
{
    if(count($this->findByTitle($data['name'])) > 0) {
        //.. your code
    } else {
        //.. your code
    }
}

Explanation:

This:

if(mysqli_num_rows($data['name']) > 0)

..is not working to find database entries by name. To do this you can use codeigniters built in model functions and benefit from the MVC Pattern features, that CodeIgniter comes with.

I wrapped the actual findByName in a function so you can adapt this to other logic and use it elswehere later on. This function uses the query() method.

Read more about CodeIgniters Model Queries in the documentation.


Sidenote: mysqli_num_rows is used to iterate find results recieved by mysqli_query. This is very basic sql querying and you do not need that in a MVC-Framework like CodeIgniter. If you every appear to need write a manual sql-query, even then you should use CodeIgniters RawQuery methods.

Jonathan
  • 1,955
  • 5
  • 30
  • 50