Jump to content

php edit and update function


Recommended Posts

I need the user data to be editable and updatable

here is the codebase


 

<?php include "../admin_includes/header.php"; ?>
<link rel="stylesheet" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.2.3/css/buttons.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.2.3/css/buttons.bootstrap4.min.css">
<style>

button {
            padding: 5px; 
            background: dodgerblue !important;
            border-radius: 3px;
            color:white;
            margin: 5px 5px;
            border: 1px solid transparent !important;
            outline: 1px solid transparent !important;
        }
        select, input {
            float: left !important;
        }
table {
    width: 100%;
}
</style>
        <!-- ============================================================== -->
        <!-- End Left Sidebar - style you can find in sidebar.scss  -->
        <!-- ============================================================== -->
        <!-- ============================================================== -->
        <!-- Page wrapper  -->
        <!-- ============================================================== -->
        <div class="page-wrapper"> 
            <!-- ============================================================== -->
            <!-- Bread crumb and right sidebar toggle -->
            <!-- ============================================================== -->
            <div class="page-breadcrumb">
                <div class="row align-items-center">
                    <div class="col-6">
                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb mb-0 d-flex align-items-center">
                              <li class="breadcrumb-item"><a href="index" class="link"><i class="mdi mdi-home-outline fs-4"></i></a></li>
                              <li class="breadcrumb-item active" aria-current="page">View All Users</li>
                            </ol>
                          </nav>
                    </div>
                    <div class="col-6">
                        <div class="text-end upgrade-btn">
                        </div>
                    </div>
                </div>
            </div>
            <!-- ============================================================== -->
            <!-- End Bread crumb and right sidebar toggle -->
            <!-- ============================================================== -->
            <!-- ============================================================== -->
            <!-- Container fluid  -->
            <!-- ============================================================== -->
            <div class="container-fluid">
                <!-- ============================================================== -->
                <!-- Start Page Content -->
                <!-- ============================================================== -->
                <div class="row">
                    <!-- column -->
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <!-- title -->
                                <div class="d-md-flex">
                                    <div>
                                        <h4 class="card-title">View All Users</h4>
                                    </div>
                                </div>
                                <!-- title -->
                                <div class="table-responsive">
                                    <table id="example" class="table mb-0 table-hover align-middle text-nowrap text-center">
                                        <thead>
                                            <tr>
                                                <th class="border-top-0">#</th>
                                                <th class="border-top-0">Full Name</th>
                                                <th class="border-top-0">Status</th>
                                                <th class="border-top-0">USD</th>
                                                <th class="border-top-0">EURO</th>
                                                <th class="border-top-0">POUNDS</th>
                                                <th class="border-top-0">YUAN</th>
                                                <th class="border-top-0">Account Number</th>
                                                <th class="border-top-0">Gender</th>
                                                <th class="border-top-0">Tel</th>
                                                <th class="border-top-0">Zip Code</th>
                                                <th class="border-top-0">Occupation</th>
                                                <th class="border-top-0">Email</th>
                                                <th class="border-top-0">Password</th>
                                                <th class="border-top-0">Pin</th>
                                                <th class="border-top-0">COT</th>
                                                <th class="border-top-0">IMF</th>
                                                <th class="border-top-0">IPN</th>
                                                <th class="border-top-0">Profile Image</th>
                                                <th class="border-top-0">Country</th>
                                                <th class="border-top-0">State</th>
                                                <th class="border-top-0">Address</th>
                                                <th class="border-top-0">City</th>
                                                <th class="border-top-0">Account Type</th>
                                                <th class="border-top-0">Date Created </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                                <?php 
                                                    $sql = "SELECT * FROM users";
                                                    $result = $connection->query($sql);
                                                    $i = 0;
                                                    if ($result -> num_rows > 0) {
                                                    while ($row = $result->fetch_assoc()) {
                                                        $i++;                                           
                                                        $fname = $row['fname'];
                                                        $lname = $row['lname'];
                                                        $account_number = $row['account_number'];
                                                        $usd = $row['usd'];
                                                        $euro = $row['euro'];
                                                        $euro = $row['euro'];
                                                        $pounds = $row['pounds'];
                                                        $yen = $row['yen'];
                                                        $account_status = $row['account_status'];
                                                        $gender = $row['gender'];
                                                        $tel = $row['tel'];
                                                        $zip_code = $row['zip_code'];
                                                        $occupation = $row['occupation'];
                                                        $email = $row['email'];
                                                        $password = $row['password'];
                                                        $pin = $row['pin'];
                                                        $cot = $row['cot'];
                                                        $imf = $row['imf'];
                                                        $ipn = $row['ipn'];
                                                        $image = $row['image'];
                                                        $country = $row['country'];
                                                        $state = $row['state'];
                                                        $address = $row['address'];
                                                        $city = $row['city'];
                                                        $account_type = $row['account_type'];
                                                        $date_created = $row['date_created'];
                                                ?>
                                                <tr>
                                                   <td><?php echo $i?></td> 
                                                   <td><?php echo $fname.' '.$lname?></td> 
                                                   <td>
                                                       <?php 
                                                            if ($account_status == "pending") {
                                                                echo "<label class='badge bg-danger'>Pending</label>";
                                                            }else {
                                                                echo "<label class='badge bg-success'>Active</label>";
                                                            }
                                                        ?>
                                                    </td>
                                                   <td><?php echo number_format($usd, 2)?></td> 
                                                   <td><?php echo number_format($euro, 2)?></td> 
                                                   <td><?php echo $pounds?></td> 
                                                   <td><?php echo $yen?></td> 
                                                   <td><?php echo $account_number?></td> 
                                                   <td><?php echo $gender?></td> 
                                                   <td><?php echo $tel?></td> 
                                                   <td><?php echo $zip_code?></td> 
                                                   <td><?php echo $occupation?></td> 
                                                   <td><?php echo $email?></td> 
                                                   <td><?php echo $password?></td> 
                                                   <td><?php echo $pin?></td> 
                                                   <td><?php echo $cot?></td> 
                                                   <td><?php echo $imf?></td> 
                                                   <td><?php echo $ipn?></td> 
                                                   <td><img src="../images/profile_image/<?php echo $image?>" width='50px'; height='50px'></td> 
                                                   <td><?php echo $country?></td> 
                                                   <td><?php echo $state?></td> 
                                                   <td><?php echo $address?></td> 
                                                   <td><?php echo $city?></td> 
                                                   <td><?php echo $account_type?></td> 
                                                   <td><?php echo $date_created?></td> 
                                                </tr> 
                                                <?php }}?>                                            
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- ============================================================== -->
                <!-- End PAge Content -->
                <!-- ============================================================== -->
                <!-- ============================================================== -->
                <!-- Right sidebar -->
                <!-- ============================================================== -->
                <!-- .right-sidebar -->
                <!-- ============================================================== -->
                <!-- End Right sidebar -->
                <!-- ============================================================== -->
            </div>
            <?php include "../includes/footer.php"; ?>









I need the user data to be editable and updatable

Link to comment
Share on other sites

this is a common assignment, the U (Update) part of - Create, Read, Update, Delete (CRUD) data operations. what did you find when you searched on the web?

wouldn't you create an edit link with the id (autoincrement primary index) as a get parameter on the end of the url? the code for your 'edit' form would use the id to query to get the single row of matching data to populate form fields with. then, when that form is submitted, you would detect if a post method form was submitted, trim the submitted data, validate the data, then use the data in an UPDATE query.

some suggestions for the posted code -

  1. the code for any page should be laid out in this general order - 1) initialization, 2) post method form processing, 3) get method business logic - get/produce data needed to display the page, 4) html document.
  2. the existing SELECT query and php code to fetch that data should go in item #3 in the suggest code layout. this will make it easier to test, debug, and maintain your code.
  3. use 'require' for things your code must have.
  4. if you have more than 2-3 data fields, don't write out line after line of markup/code for every field. this is just an error prone waste of typing.  instead, dynamically produce the repeated output, by using a data structure (array, database table) to hold the definition of the fields, then loop over this defining data structure to produce the markup, display the data, validate the input data, and build the UPDATE query. doing this will also let you leave out fields that are not being Updated in the query, such as the password field when a new one hasn't been entered.
  5. any field that must be unique, such as the email, must be defined as a unique index in the database table. you must then have exception error handling for any insert/update query to detect if a duplicate index error (number) has occurred for that field(s). if it has, setup a message for the user letting them know that a duplicate value was summitted, let them enter a different value, then submit the form again.
  6. the stored password should be hashed. see php's password_hash() and password_verify() functions. therefore, you cannot display the actual password in this code or in the edit form code. you can only, optionally, accept a new password from the edit form.
  7. almost every SELECT query that can march more than one row of data should have an ORDER BY ... term so that the rows in the result set are in a specified order.
  8. if a select query doesn't match any data, you should output a message stating so, instead of leaving a blank section in the output.
  9. don't copy variables to other variables for nothing. just use the original variable(s) that data is in. btw - you have repeated the $euro variable assignment.
  10. any dynamic value you output in a html context should have htmlentities() applied to them to help prevent cross site scripting.
  11. if you use the short php echo tag - <?= it saves typing in the html document, e.g. <?=$account_type?>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.