codeigniter check insert

To check if an insert operation was successful in CodeIgniter, you can follow these steps:

  1. Load the CodeIgniter database library: php $this->load->database(); This step ensures that the database library is loaded, allowing you to interact with the database.

  2. Define the data to be inserted: php $data = array( 'column1' => 'value1', 'column2' => 'value2', // add more columns and values as needed ); Here, you define the data that you want to insert into the database. Each column and its corresponding value is specified in the $data array.

  3. Perform the insert operation: php $this->db->insert('table_name', $data); This code inserts the data into the specified table. Replace 'table_name' with the actual name of your table.

  4. Check if the insert operation was successful: php if ($this->db->affected_rows() > 0) { // Insert successful } else { // Insert failed } The affected_rows() function returns the number of affected rows from the previous query. If it returns a value greater than 0, it means that the insert operation was successful.

You can use an if statement to check if the insert was successful and execute the appropriate code based on the result.

That's it! By following these steps, you can check if an insert operation was successful in CodeIgniter.