chetans9 / core-php-admin-panel

An Admin panel written in core php with CRUD, filters and pagination.
http://freecs9.epizy.com/core-php-admin
283 stars 141 forks source link

Update record #2

Closed Zeroema closed 6 years ago

Zeroema commented 6 years ago

Hello,

First thanks for this wonderful php admin panel. One question, when I create a new element in the side bar, and I tried to make the update option works, always show the same record not matter wich row I selected. What I´m doing wrong?

chetans9 commented 6 years ago

make sure you have added query string parameters on links to update records e.g update.php?customer_id=61&operation=edit .

keep "name" attribute of input type same as database column name.

then use $data_to_update = filter_input_array(INPUT_POST); $db->where('id',$customer_id); $db->update('customers', $data_to_update);

Zeroema commented 6 years ago

Hello Chetans9,

Thank you for your support and quick answer.

Done. I found the problem. I changed the value of "id" from a number to a Mac address but the FILTER_VALIDATE_INT was causing the problem.

Something new that I detected is that when you click on save button to save the changes in the MySQL table, in the php the changes goes back to the original.

e.g edit store value after click in save button

chetans9 commented 6 years ago

Ok. I got it. Thanks for pointing it out. After Updating/Inserting Redirect to same page and show a session flash message.

Like this :

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $data_to_update = filter_input_array(INPUT_POST);
    $db->where('id',$customer_id);
    $stat = $db->update('customers', $data_to_update);

    if($stat){
        $_SESSION['success'] = "Customer updated successfully!";
        header('location: customers.php'); //Redirect after successfull update
        exit();
    }
}

and whatever page you are redirecting Add something like this

if(isset($_SESSION['success'])) { echo '<div class="alert alert-success alert-dismissable"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <strong>Success! </strong>'. $_SESSION['success'].' </div>'; unset($_SESSION['success']); }

I was a newbie when I built this. I will fix it in later commits..