The save button is not functioning properly

Currently, I am in the process of creating an order form. One of the key functionalities I have been working on is the "save changes function". This feature allows users to make changes to the table and then update the database by clicking a specific button.

In addition, the "Bestelling toevoegen" section serves as the form for adding new entries. When entering the required information and selecting "Toevoegen", a new record will be displayed in the table above.

Despite these developments, I have encountered an issue with saving changes to the "Status" field.

Below is the comprehensive code snippet:


$dbname = "localhost";
$dblogin = "root";
$dbpass = "";
$dbtable = "bestelformulier";

$con=mysqli_connect("$dbname","$dblogin","$dbpass","$dbtable");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM overzicht");

echo "<form name='wijzigen' method='post'>";
echo "<table align='center' width='700px' border='2'>
<tr>
<th>Ordernr</th>
...

Answer №1

It seems like the issue you are facing is due to outputting multiple select elements with the same name, status. To resolve this problem, consider updating the following line of code:

echo "<td><select name='status'>

to:

echo "<td><select name='status[$ordernr]'>

This adjustment will create an array of status elements with order numbers as keys. Next, modify the code snippet below:

$status = $_POST['status'];

if(isset($_POST['wijzigen'])) {
$query = mysqli_query("UPDATE overzicht SET status='$status' WHERE ordernr='$ordernr'");
}

to:

$statuses = $_POST['status'];

if(isset($_POST['wijzigen'])) {
    foreach($statuses as $ordernr => $status)
    {
        if($status != "")
            $query = mysqli_query($con, "UPDATE overzicht SET status='$status' WHERE ordernr='$ordernr'");
    }
}

Answer №2

Here is some advice for improving your code:

$query = mysqli_query("UPDATE overzicht SET status='$status' WHERE ordernr='$ordernr'");

Make sure that your $ordernr is not empty to successfully update existing records. Pass the ordernr via GET or POST in the form.

If you want to update multiple order statuses simultaneously, consider passing all ordernrs and their statuses as an array or use AJAX for more efficient processing.

Lastly, ensure you protect against SQL injection attacks. If this code goes live, it will be vulnerable to hacking attempts on your database.

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

What could be causing Laravel's execute() method to return duplicate results?

As a novice Laravel developer trying to export query results to a CSV file, I'm encountering confusing duplicate values in the output. Here is my code: $connection = fopen('/BSTablespace/someFile.txt', 'w'); $users = DB::table(&a ...

What steps can be taken to address an unusual conflict arising between a form post and an ajax post

One web page features the use of ajax for editing existing values. This is achieved by utilizing jQuery Inline Edit to post new data, update records, and return with success. The process works well initially. Subsequently, I implemented the functionality ...

Leveraging passport.js and direct SQL queries for authentication purposes

Currently tackling a project with an issue that needs resolving. I am looking to integrate user login logic utilizing the Passport API, but struggling to grasp its functionality, especially when implementing it using plain SQL queries. While scouring thr ...

Mastering Regular Expressions with PHP

Hello, I am currently working on creating a Regular Expression that will replace any word enclosed in pound signs (#) with a URL. I want it to function similar to Twitter hashtags, but my current expression only works for English words. If a post is made ...

How can I dynamically add PHP code to MySQL using POST requests?

I have a textarea form where I input text and send it to PHP using POST. PHP receives the POST data and then inserts it into MySQL like so: $content = $_POST['content']; $stmt = $mysqli->prepare("INSERT INTO posts (content) VALUES (?)"); $st ...

Loading jQuery on an ajax request

The loader is working with the code now, but it is not replacing and calling the URL. The ajax url call should be placed within searchable. <button onclick="myFunction()">LOAD</button><br /><br /> <div class="spinner bar hide" ...

Error in PHP XPath query occurs following DOM adjustment

Every time I attempt to XPath-query a recently created node, I encounter the message PHP Notice: Trying to get property of non-object in .... The structure of my XML file is as follows: <products xmlns='http://example.com/products'> ...

Show individual row information within a bootstrap modal upon clicking the edit button

As a beginner in programming, I am attempting to use bootstrap modal to showcase row data from a mysql table within the modal window. Following the advice found on stackoverflow regarding "Pull information from mysql table to bootstrap modal to edit" didn ...

Troubleshooting a GD library issue: resolving the white screen dilemma

I'm currently facing an issue with the GD PHP library. When I attempt to send an image to a localhost VM, the image displays properly on the screen. However, when trying to do the same on a VPS, the image does not generate and the screen remains white ...

Choosing data based on specific conditions in a database using PHP when the $_POST array is empty and contains a

I am having trouble selecting data from my database using PHP and an ajax call function. In the select function, I am using $_POST to retrieve data from a textbox. Even though I have set up a PHP and ajax call function on my page, something seems to be goi ...

Unable to execute ajax using php

Having just started learning php and ajax, I am trying to execute a simple code of ajax with php. However, the code is not functioning as expected. My goal is to load some text on the page when an onchange event occurs. source code: ajax.php <select i ...

The value of ajax data at index 0 is not defined

When using AJAX to retrieve the number of rows from a SQL query in PHP, the JSON return shown in the Firefox Network tab is [{"number":2}], with the number 2 being without quotes. However, when trying to access the value (2) using data[0]["number"] or dat ...

Retrieve specific file types from directory

As a beginner in PHP, I'm looking to extract all available .jpg images from a specific folder. I attempted the following code but it's not functioning correctly. $type = 'jpg'; $pics = opendir('admin/upload/server/php/files/th ...

Discover the seamless method of transferring data from an HTML5 form to a PHP page for processing without the need to refresh the page or manually navigate to the PHP file

When creating a website, I encountered an issue with a form that requires the user to select three options from select elements and then click a search button to search the database. The problem is that PHP does not retrieve the form data unless the button ...

Utilizing long polling technique with jQuery/AJAX on the server end

Currently, I am facing an issue with long polling on a single page that contains multiple pages. The problem arises when a new request is made while a previous request is still processing. Even though I attempt to abort the previous request, it completes b ...

What is the best way to display a chosen item in a text input field?

https://i.stack.imgur.com/Qe5Ds.png Looking to create a similar design, but lacking the necessary expertise. What steps should I take or what is the specific term for this style? Seeking assistance in implementing this using jQuery, PHP, or JavaScript. A ...

Tips for handling binary data retrieved from a SQL query (such as LONGBLOB type) in node.js

I am trying to send binary data to the client using node.js, but I have encountered a limitation where write can only send string or Buffer. How can I successfully send binary data to the client? dbconnect.selectBinary(conn,function(result) { //resul ...

Generate an HTML webpage with dynamic content using MySQL

How can I display data from a MySQL table on an HTML page? The content in my table includes various rows that I would like to show separately on the web page. This is the structure of my table: I envision the web page layout to showcase each row individ ...

What is the best method for storing a multidimensional array in cookies using PHP?

cookie[person][name], cookie[person][id], cookie[person][age] What is the method to create a cookie with the specified structure? ...

Sending AJAX information to multiple pages

I have created an HTML page where I am struggling to pass two variables using the POST method to a PHP page. The PHP page is supposed to accept these variables and then call an API to retrieve data based on them. However, my challenge is in receiving this ...