开发者

Deleting a database record using $_POST with Codeigniter

开发者 https://www.devze.com 2023-02-06 23:26 出处:网络
I\'ve been doing it all wrong, I used to take the value from the URI segment and didn\'t realize it wasn\'t the ideal way. So I changed my approach and now have everything via a $_POST. I\'m not sure

I've been doing it all wrong, I used to take the value from the URI segment and didn't realize it wasn't the ideal way. So I changed my approach and now have everything via a $_POST. I'm not sure if I'm doing this correctly, could someone shed some light? My view contains tabular data listing items pulled from the DB. Each item has two links, "View" and "Delete." The code seems to work but was wondering if it could be coded better. I forgot that the form name wasn't unique, so when I went to go delete a record, it would always delete the newest record (the last hidden field was set).

myview.php (snippet)

<?php foreach($records as $record): ?>
    <form method="POST" name="myform<?php echo $location->id;?>" action="/location/delete">
    <a href="/location/view/<?php echo $location->id;?>">View</a> <a href="#" onclick="document.myform<?php echo $location->id;?>.submit();">Delete</a>
    <开发者_开发问答;br />
    <input type="hidden" name="location_id" value="<?php echo $location->id;?>">
    </form>
<?php endforeach ?>


Viewing/Deleting via uri id is perfectly fine, I wouldn't venture to say that using $_POST is wrong, but creating a new unique form for every delete element is terribly messy, and weighed against what you are gaining (no exposed id i guess?), I believe it is more 'correct' to use the uri for delete functions.

If you only want certain people to be able to delete certain records, handle that programmatically in the delete function itself, don't depend on the fact that the request is only sent via $_POST. This is not dependable, anyone can generate a post request.


For anyone who comes across this later, here's how I solved my issue.

In my controller I have a method called delete that checks to see if the form field was submitted via a $_POST. If there's no variable, redirect them somewhere with an error message. If the field was passed, then go through the normal checks to make sure the record can be deleted.

if(!isset($_POST['item_id']))
{
    $this->session->set_flashdata('message', 'item cannot be removed!'); 
    redirect("/item");
}


if($this->input->post('item_id')) {         
    ... code ....
    ... code ....
}


Your syntax error is with this line:

<?php foreach($records as $record): ?>
         <form method="POST" name="myform<?php echo $location->id;?>"      action="/location/delete">
         <a href="/location/view/<?php echo $location->id;?>">View</a> <a href="#" onclick="document.myform<?php echo
 $location->id;?>.submit();">Delete</a>
         <br />
          <input type="hidden" name="location_id" value="<?php echo $location->id;?>">
         </form>
      <?php endforeach ?>

You can not do looping for a form. Instead, use the following code:

   <form method="POST" name="myform<?php echo $location->id;?>" action="/location/delete">
<?php foreach($records as $record): ?>
    a href="/location/view/<?php echo $location->id;?>">View</a> <a href="#" onclick="document.myform<?php echo $location->id;?>.submit();">Delete</a>
        <br />
        <input type="hidden" name="location_id" value="<?php echo $location->id;?>">
    <?php endforeach ?>
       </form>
0

精彩评论

暂无评论...
验证码 换一张
取 消