Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Next steps would be accounting for errors and bad data</p> <ul> <li>check $id to make sure its really an integer. this could happen in the controller before calling delete, or as part of the delete method. the important point is that because the id is coming straight from the URL with no validation, you have to do something to check it before letting it near your database. </li> <li>in the model, delete_news should return true or false, to confirm that the record was found and deleted. </li> <li>in the controller, wrap $this->news_model->delete_news($id); in an IF checking if it came back true from the model, else error redirect.</li> <li>if there is an error, since you want the url to change, you can still redirect, just pass an appropriate error code like</li> </ul> <p>redirect('/news/adminerror/delete');</p> <p>then in your news controller, have a method like adminerror($errortype)<br> then based on the value of $errortype, which in this case would be a delete error, display appropriate message and view. </p> <p>EDIT</p> <p>2- in the model, inside your delete method, just put this at the end:</p> <pre><code>if ($this-&gt;db-&gt;affected_rows() == '1') {return TRUE;} else {return FALSE;} </code></pre> <p>3- in the controller, wrap in an IF, its usually easier to check if something did not work first</p> <pre><code>if( $this-&gt;news_model-&gt;delete_news($id) == false ) { // redirect to error page or whatever you need } // if its not false, then its true, which means it worked else { $this-&gt;session-&gt;set_flashdata('error_message', 'record #'. $id . ' deleted'); redirect('/news/'); } </code></pre> <p>Final quick tip for update record -- </p> <pre><code>$this-&gt;db-&gt;affected_rows() == '1' </code></pre> <p>is also used to confirm that an update happened. BUT if you do an update, lets say you are updating a blog post - and then you click to update again, but actually you didnt change any content.... then this will come back as false. the update did go through, but since the content was the same, the database was not changed or 'affected'. </p> <p>so if you use that method to confirm an update - there might be a cooler way to do this, but i just send something along with the update content that will always be different - date/time, random number, etc. </p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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