php - Deleting a database record using codeigniter -
OK, struggling here, I am very new to codeigniter and am quite new to PHP. I want to delete a record from DB. I'm getting an error with the controller here. My code is
Controller:
Public function removeitem () {$ this-> Load-> Assistant (array ('form', 'url')); $ This- & gt; Load-> View ('vRemove'); } Public event doremove () {$ this- & gt; Load-> Model ("mphoto"); $ This- & gt; Mphoto-> RemoveItem ($ id); $ This- & gt; Load-> View ('removed_success'); } Model:
public function removeItem ($ id) {$ this- & gt; Db- & gt; Where ('ProductID', $ id); $ This- & gt; Db- & gt; Delete ('simple'); } See more:
& lt ;? Php echo form_open_multipart ('site / doremove') ;? & Gt; & Lt; P & gt; Are you sure you want to delete this item? & Lt; / P & gt; & Lt; A href = "& lt ;? php echo base_ url () ;? & gt; index.php / site / admin" & gt; & Lt; Input type = "button" value = "cancel" /> & Lt; / A & gt; & Lt; Div & gt; & Lt; Input type = "submit" value = "submit" /> & Lt; / Div & gt; & Lt; / Form & gt; & Lt ;? Php? & Gt; I am getting the error:
encountered a PHP error
Seriousness: Notification
Message: Undefined
Line Number: 114
What is this line:
$ this-> Mphoto-> RemoveItem ($ id); Edit: The error no longer exists after changing the following code in the controller, but now the problem is that the item from the database is not being deleted.
public work doremove () {$ this-> Load-> Model ("mphoto"); $ Id = $ this- & gt; Input-> Obtain ('ProductID'); $ This- & gt; Mphoto-> RemoveItem ($ id); $ This- & gt; Load-> View ('removed_success'); }
Any help is appreciated. $ id
in the doremove () function of the controller Code> is undefined You must give value to id to see the controller; URI segment parameter (GET)
Pass ID through an URI segment. For example: 2 will be passed as the parameter in the doremove function in the site controller. See & lt; P & gt; Are you sure you want to delete this item? & Lt; / P & gt; // Change the id2 of the product you want to delete - you can dynamically move it to & lt; A href = "echo site_url ('site / doremove / 2');" & Gt; Yes & lt; / A & gt; // Add Cancel button etc. ... Administrator public function doremove ($ id = null) {if ($ Id === null) {// ID is not set - Show a suitable message, redirect, whatever you want ...} and {$ this- & gt; Load-> Model ("mphoto"); // Is the removal operation successful? If ($ - it - gt; mphoto-> gtk; remove item ($ ID)) $ - THIS- & gt; Load-> View ('deletedSuccess'); And $ this- & gt; Load-> View ('deleted_file'); // > & lt ;? Php echo form_open ('site / doremove') ;? & Gt; & Lt; P & gt; Are you sure you want to delete this item? & Lt; / P & gt; Change // 2 to the id of the product that you want to delete & lt; Input type = "hidden" name = "product_id" value = "2" /> & Lt; Input type = "submit" value = "submit" /> & Lt; / Form & gt; Public work doremove () {$ id = $ this- & gt; Input-> Post ( Controller Product ID '); If ($ id) {$ this- & gt; Load-> Model ("mphoto"); // Is the removal operation successful? If ($ - it - gt; mphoto-> gtk; remove item ($ ID)) $ - THIS- & gt; Load-> View ('deletedSuccess'); And $ this- & gt; Load-> View ('deleted_file'); // or whatever you want} and {// ID is not set - show a suitable message, redirect, whatever you want ...}}
It is also a good idea to check whether the work done on the database is successful or not.
Model Trying to delete the public function removeItem ($ id) {// line $ -> this-> DB- & gt; Where ('product id', $ id); $ This- & gt; Db- & gt; Delete ('simple'); // Was the row deleted? If ($ this-> db-> influential_ rows () == 1) True TRUE; And returned the wrong; }
Comments
Post a Comment