Note that there are some explanatory texts on larger screens.

plurals
  1. POCall to member function error in CodeIgniter
    primarykey
    data
    text
    <p>I'm writing a code which use ajax to delete a specific record in the table. But every time I try to delete a record, it gives me the following error.</p> <blockquote> <p>Fatal error: Call to a member function delete_specific_record() on a non-object in <b>C:\xampp\htdocs\abcsystem\application\controllers\fuel_types.php</b> on line <b>118</b><br /></p> </blockquote> <p>I tried almost everything on the internet as a solution and I'm still getting nowhere. I will add the relevant code blocks for reference.</p> <p>In JS,</p> <pre><code>//this function will run when fuel delete button is pressed $("#fueltypes .delete").click(function(event) { //prevent default action being triggered event.preventDefault(); //store relevant data fields to variables var base_url = $("#base_url").val(); var fuelTypeId = $(this).siblings("input[type=hidden]").val(); //create the data streem datastreem = ({fuelTypeId:fuelTypeId}); $.ajax({ type:'POST', url:base_url+"index.php/fuel_types/ajax_delete_fuel_type", data:datastreem, success:function(response) { if(response==1) { alert("Fuel type deleted"); } else { alert("Fuel type deletion failed"); } } }) }); </code></pre> <p>In controller(fuel_types.php),</p> <pre><code>class Fuel_types extends CI_Controller { //this function will run when admin try to delete a fuel type public function ajax_delete_fuel_type() { //save ajax sent data into variables $fuelTypeId = $this-&gt;input-&gt;post("fuelTypeId"); //load vehicle model $this-&gt;load-&gt;model("Vehicles_model"); //create the recordId array $recordId = array('fuel_type_id' =&gt; $fuelTypeId); //call the function to check if record exists $results = $this-&gt;Vehicles_model-&gt;get_specific_record($recordId); //if records available if(($results-&gt;num_rows)&gt;0) { echo "0"; } //if no record is available in the vehicle table else { //load fuel type model $this-&gt;load-&gt;model("Fuel_types_model"); //create the recordId array $recordId = array('fuel_type_id' =&gt; $fuelTypeId); //call function to delete records in the model $delResults = $this-&gt;Fuel_types_model-&gt;delete_specific_record($recordId); //if record deletion is successful if($delResults&gt;0) { echo("1"); } //if it fails else { echo("0"); } } } } </code></pre> <p>and finally at the model(Fuel_types_model.php),</p> <pre><code>class Fuel_types_model extends CI_Model { function __construct() { parent::__construct(); } //this function will delete record(s) depend on the recordId public function delete_specific_record($delRecordId) { $this-&gt;db-&gt;where($delRecordId); $this-&gt;db-&gt;delete("fuel_types"); $delResults = $this-&gt;db-&gt;affected_rows(); return $delResults; } } </code></pre> <p>I'm really confused without knowing what I have done wrong. FYI, I have autoloaded database in config/autoload.php file.</p> <p>I have only included relevant functions of the controller and the model. Other functions which are associate with this controller and model - such as data retrieval and update functions are working just fine. Only problem is </p> <pre><code> //call function to delete records in the model $delResults = $this-&gt;Fuel_types_model-&gt;delete_specific_record($recordId); </code></pre> <p>this line.</p> <hr> <p>(Edit) FYI, I have below included the complete error message</p> <blockquote> <p>A PHP Error was encountered</p> <p><p>Severity: Notice</p> <p>Message: Undefined property: Fuel_types::$fuel_types_model</p> <p>Filename: controllers/fuel_types.php</p> <p>Line Number: 118</p></p> <p><br /> <b>Fatal error</b>: Call to a member function delete_specific_record() on a non-object in <b>C:\xampp\htdocs\abcsystem\application\controllers\fuel_types.php</b> on line <b>118</b><br /></p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload