Checkbox form for updating entries in the SQL database table

My goal is to make changes to a MySQL table using an HTML form.

Here's a snippet of the form:

<div id="caricoModal" class="modal fade">
    <div class="modal-dialog">
        <form method="post" id="employeeForm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"><i class="fa fa-plus"></i> Edit User</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="tipoOp" class="control-label">Operazione:</label>
                        <select id="tipo" name="tipo" class="form-control">
                            <option value="carico">Carico</option>
                            <option value="scarico">Scarico</option>
                        </select>
                    </div>
                    <div class="form-group">
                    <!-- Record list -->
                    <table border='0' style='border-collapse: collapse;' >
                        <tr style='background: whitesmoke;'>
                            <!-- Check/Uncheck All-->
                            <th>Username</th>
                            <th>Stato</th>
                        </tr>

    <?php 
    $query = "SELECT * FROM users where stato='0'";
    $result = mysqli_query($conn,$query);

    while($row = mysqli_fetch_array($result) ){
        $id = $row['id'];
        $stato = $row['stato'];
    ?>
                        <tr>
                            <!-- Checkbox -->
                            <td><input type='checkbox' name='update[]' value='<?= $id ?>' ></td>
                        
                            <td><?= $id ?></td>

                            <td><input type='number' name='stato_<?= $id ?>' value='<?= $stato ?>' ></td>

                        </tr>
    <?php
    }
    ?>
            </table>
    </div>

    <div class="form-group">                            
        <label for="name" class="control-label">Data Formulario</label>
        <input type="date" class="form-control" id="datafr" name="datafr" placeholder="Data Formulario" value="<?php echo date('Y-m-d'); ?>" required>          
    </div>

When I submit the dialog form, I trigger a method to add a new row in my table and run a query based on checkbox selections.

Below is the code for adding a new row:

public function addCarico(){
            
    $insertQuery = "INSERT INTO ".$this->empTable." 
                (data_fr, data_trasporto, codice_eer, stato_fisico, 
                classi_pericolo, formulario, produttore, trasportatore, 
                destinatario, quantità, note, carico, stato_carico) 
            VALUES ('".$_POST["datafr"]."', '".$_POST["datatr"]."', 
                    '".$_POST["eer"]."', '".$_POST["statof"]."', 
                    '".$_POST["pericolo"]."', '".$_POST["fr"]."', 
                    '".$_POST["prod"]."', '".$_POST["trasp"]."', 
                    '".$_POST["dest"]."', '".$_POST["qta"]."', 
                    '".$_POST["note"]."', '1', '0')";
    $isUpdated = mysqli_query($this->dbConnect, $insertQuery);  
    print_r($_POST['update']);
}

I attempted to include this piece of code:

if(isset($_POST['update'])){
    foreach($_POST['update'] as $updateid){

        $stato = $_POST['stato_'.$updateid];

        if($stato !=''){
            $updateUser = "UPDATE users SET 
                stato='1' 
            WHERE id=".$updateid;
            mysqli_query($con,$updateUser);
        }
        
    }
}

However, I'm puzzled by why the table isn't updating after submitting the form.

Answer №1

To troubleshoot any issues, I recommend reviewing the MySQL error log and potentially enabling the query log temporarily by setting SET GLOBAL general_log = 'ON'. Additionally, take @RiggsFolly's advice to enable thorough debugging in order to identify any errors that may surface. You can also insert an echo 'your desired message' within the if isset($_POST) statement.

Answer №2

Verify different steps to ensure the update query is functioning properly.

  1. Confirm that the variable $ _POST ['update'] is being received

  2. Display the update query that will be executed

  3. Validate the correctness of the query

If all these checks pass, it may indicate a potential issue with the database connection.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can you provide step-by-step instructions on how to construct phpalanger phpc

Attempting to compile https://github.com/DEVSENSE/Phalanger, but encountering build errors after the process. Could it be due to using VS 2015, possibly a version compatibility issue? https://i.stack.imgur.com/Q6qXv.png ...

The vanishing AJAX button

Currently, I am working on a web app in Laravel that includes a simple textarea within a form. This form allows users to input basic markdown text, and my goal is to display the data with both HTML tags and normal formatting. However, I encountered an issu ...

Query Laravel 5 Eloquent to fetch records from Table 1 that do not have a corresponding relationship in Table 2 where the Table 2 id is equal to x

I am in search of a method similar to has() that will allow me to accomplish the following: Retrieve all users from the users table who do not have a link with the subscriptions table when subscriptions.id = x For example, a user may be linked to subscri ...

What is the best way to transfer PHP form data to an Angular2 application?

As I am still getting familiar with angular2/4, please bear with me if I overlook something obvious. I am currently working on updating a booking process using angular2/4. Initially, the booking procedure commences on a php website, and once some basic in ...

Utilizing JQuery AJAX and PHP for Data Encoding

My webpage is encoded in HTML with the charset ISO-8859-2. All content on the page adheres to this charset and appears correctly. However, I am facing an issue when making an Ajax call to a PHP server. When I send the character á and receive the same valu ...

In PHP, extract the initial three characters from each item within a foreach loop

In the following code snippet, I am trying to extract data from a JSON array and store it in a variable: $arr = json_decode($jsondata, TRUE); $arr2 = $arr['items']['item']; echo '<ul>'; foreach ($arr2 as $val) { echo &a ...

Redis enhances performance in PHP applications by alleviating bottlenecks commonly associated with PHP

I have successfully installed Redis 2.4 on my Ubuntu Desktop 11.10, equipped with 8 cores and 8 GB of RAM. Upon using the redis-benchmark tool, I consistently achieve a performance of 100K SETS and GETS per second with a 4096 byte package. Comparatively, ...

Displaying every outcome of a PHP script with AJAX

When sending data to a PHP file using AJAX and receiving a response, I encounter the following code: while($i<14){ echo $i.'<br />'; $i++;} This code returns 14 repetitions. Upon calling data from my webpage using the AJAX method, it ...

Permission for resizing images after they are uploaded to the platform

Currently, I am working on a user's profile page where the user can upload their profile picture using a file dialog. However, when the file is moved to my local server's folder, it only receives permissions as 0644. I am looking to resize this ...

What is the method for enabling decimal quantities in the get_refreshed_fragments function within Woocommerce?

I wrote a custom code snippet that allows me to use decimal quantities on the My Cart page: function customize_quantity_field() { // Set minimum value for quantity field (default = 1) add_filter('woocommerce_quantity_input_min', 'min ...

The challenge with using prepared statements in PHP/MySQL for inserting data into a form

Recently, I encountered an issue with the code below that takes information from a form and sends it to MySQL. Previously, everything was working fine for category, contents, date, and user ID fields. However, after adding a new column called 'secleve ...

Troubleshooting Problem with PHP and MYSQL's Insert_ID Function

Currently, I am fetching data from a JSON file using cURL and attempting to add the data id to my "tblcustomfieldsvalues" MySQL table based on the entries in the "tblclients" table. Despite utilizing mysqli_insert_id, I have not been successful in achievin ...

What is the best way to integrate my custom JavaScript code into my WordPress theme, specifically Understrap?

I am looking to enhance my website with a sticky navbar positioned directly under the header, and I want it to stick to the top of the page as users scroll down. Additionally, I want the header to disappear smoothly as the user scrolls towards the navbar. ...

Mysql coding for search engines utilizing names

I am trying to create a search engine in PHP that searches for people using their first and last names, each stored in separate columns. Currently, I am using the following SQL query: SELECT * FROM users WHERE first_name OR last_name LIKE '$search_ ...

Performing AJAX requests within AJAX requests without specifying a callback function for success

Upon reviewing this discussion jQuery Ajax Request inside Ajax Request Hello everyone, I'm in need of some clarification on a particular scenario. I recently took over the code from a former member of my development team and noticed that they have ma ...

Retrieve the result set rows and store them in an array as individual objects

I'm attempting to fetch data from a MySQL database using Angular and PHP. My Angular code looks like this: $http({ url: "http://domain.com/script.php", method: "POST", headers: {'Content-Type': 'applica ...

Login Troubles on Android

I am encountering an issue while working on an Android app. Specifically, when attempting to log in, I am receiving the following error in the log cat: 11-30 21:23:18.694: E/JSON Parser(1914): Error parsing data org.json.JSONException: Value <br of typ ...

methods for transferring javascript variables to modal

<div> <h5> <a href="<?php echo base_url(); ?>/vendor/home/projects" >Return to Projects</a> </h5> <div> <div class="container"> <div class="row"> ...

What is the best way to customize the cache "get" method in Laravel?

Need assistance in overriding the method get() from the class Illuminate\Cache\Repository. Here's my code snippet: <?php namespace App\Illuminate\Cache; use Illuminate\Cache\Repository as BaseRepository; class Reposi ...

Is it possible to utilize the 'username' global variable in expression engine for querying records from a MySQL database?

I am currently in the process of developing a form on a school website using Expression Engine, allowing parents to easily book after-school care for their children through the platform. Upon logging in with their unique family ID, which corresponds to the ...