Tips for updating multiple database columns in a single query

While following a tutorial on updating a database with ajax, I encountered a situation where the tutorial wanted to create a new row for each update, but I needed to update an existing row instead. I managed to modify the code to suit my requirements, but now I want to update more than one column in the same row.

My form includes two values: Show Announcement and Announcement Content. In the text field, I can update Show Announcement to either 0 or 1 (true or false), and in Announcement Content, I can input the desired text.

The issue arises when I try to add

&& isset($_POST['anncontent']) && $_POST['anncontent']!=''
in the if statement at line 19 (marked within the code) and include another SET option in the variable $sql (
SET announcement=('".addslashes($_POST['anncontent'])."')
). This results in a status code 500 error and neither of these content updates are reflected in the database.

Here is the relevant snippet from submit.php:

<?php
// PHP code snippet

In my index.php, the form layout allows me to update both fields as required:

// HTML code snippet

I suspect that there might be an issue with my SQL query. I attempted adding another $sql call with only the SET announcement clause, but it did not resolve the problem, leading to the same error code.

When attempting to update only the Show Announcement field, I receive an "Error While Updating" message, whereas trying to update both fields leaves me stuck with an "Updating..." message displayed without progress. You can view a screenshot of the page here.

If additional information is required to diagnose the issue, please let me know.

Answer №1

To ensure that the input fields are not empty, I recommend using the !empty() function, which checks for both conditions at once. Additionally, you can bind two parameters by following this example:

if (!empty($_POST['name']) && !empty($_POST['announcement'])) {
$stmt= $pdo->prepare("UPDATE settings SET name = :name, announcement = :announcement");
$stmt->execute([
    'name' => $_POST['name'],
    'announcement' => $_POST['announcement']
]);}

For more information on PDO, check out this useful resource.

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

The successful execution of $.ajax does not occur

Starting out with ajax, I wanted to create a simple add operation. Here is the code that I came up with: HTML: <!doctype html> <html> <head> <title>Add two numbers</title> <meta content="text/html;charset=utf-8" h ...

The combination of Ajax, JavaScript, PHP/HTML, and dynamic variables

Hey there, I'm currently working on a game development project and have encountered what seems to be a scope issue. The issue arises in my js file when a player clicks on the card they want to play: It starts in my js file:</p> <pre>&l ...

Encountering an issue with Ajax code on initial execution

I've implemented an Ajax script on my PHP page to execute a PHP script upon clicking a button. However, the issue I am facing is that when I initially load the web page, the script does not run. But, if I click back and repeat the process, the script ...

Having issues with the onclick() function not functioning properly with Jquery?

Yesterday, I successfully integrated some Jquery code into my website. However, when I attempted to add more code today for a new feature, everything seemed to stop working. Even the code that was functioning perfectly yesterday has now ceased to work. The ...

Removing a user from QuickBlox with the help of CodeIgniter

I am facing an issue in my controller where I cannot figure out how to delete a Quickblox user. Can someone provide guidance on the code needed for this task? Here is my controller function: public function delete_customer($id) { $query = $thi ...

Utilizing AJAX to define the attributes of an object

As a beginner in OOP, I am trying to create an object using an ajax request. My goal is to retrieve 'responseArray' in JSON format and then manipulate it. function address(adres) { this.address_string = adres; var self = this; $.ajax({ typ ...

Having trouble obtaining data from the database and showing it on the webpage

I am encountering an issue with displaying content from a database named resources and a table within it also named resources. The code snippet causing trouble is as follows: mysql_connect('localhost', 'username', 'password', ...

When the if-else statement is executed, it showcases two strings:

Learning Experience: Unveiling My Lack of Cleverness Update: It appears that utilizing PHP in my current setup is not possible. As a result, I'll take some time to contemplate while banging my head against a wall. Despite that, thank you all for your ...

Ways to handle the ajax callback content

I'm dealing with an AJAX code that looks like this: var req = new XMLHttpRequest();<br> req.open('GET', 'http://www.example.org/', false); req.send(null);<br> if(req.status == 200)<br> var response = http_att ...

"Encountering an issue when linking the file with phpMyAdmin

I've been struggling with this issue for hours now. I'm currently working on a registration form for my website and while coding in PHP, I connected it to MySQL and the Database using this line of code (which happens to be the 6th line): $mysq ...

Script using PHP to remotely connect to a Linux server and run a Bash script

Currently, I am facing a challenge where I need to execute shell commands on our remote servers using PHP. The main goal is to install scripts through bash by having PHP execute these scripts on the server remotely. However, I have encountered an issue wh ...

Transferring cookies across subdomains

I am facing an issue with an ajax request going from one subdomain to another, for example from sub1.example.com to sub2.example.com. Despite having a cookie set for all domains (cookie domain='.example.com'), the cookie is not being sent to the ...

What is the reason for JavaScript consistently returning the initial value as the result?

My current issue involves customizing Joomla article content using a module. I am attempting to hide a div until a user clicks on an input, such as a radio button labeled Test1. Once Test1 is selected, another hidden field within the div should display the ...

Nested Ajax request fails and triggers a full page reload

My goal is to search for product information and images using a product code input on index.php. The query runs in open_first.php via an ajax post request, which works perfectly. open_first.php displays images that can be selected by clicking on them. How ...

Having trouble with PHPmailer resulting in a 504 gateway timeout error

I keep receiving a 504 gateway timeout error from my server when using phpmyadmin to individually send emails to a list of approximately 1200 users. It is crucial for me that the emails are sent out one by one, as I want to avoid exposing any email addres ...

What methods with JavaScript, Ajax, or jQuery can I apply to populate the student details?

For completing the StudentID section, please fill out the form data.cfm with the first name, last name, and Middle Name. <script language="Javascript"> $(function() { $( '#effective_date' ).datepicker(); jQuery.validator.addMetho ...

Errors in JSON.parse: which character am I not detecting?

My development environment includes PHP 5.3.5, MySQL Server 5.5.8, and jquery version 1.6. I'm using Ajax to automatically populate a dropdown list of countries, but I keep encountering an error that persists despite trying various solutions. For exa ...

Looking to retrieve the image source from a SQL database and display it in a view file using Ajax in CodeIgniter?

I am new to PHP and have successfully created an AJAX dynamic dependent dropdown in CodeIgniter. It is working fine, but now I want to show an image for the selected option when the last dropdown's option is selected. Please help me with this. View a ...

Error encountered: Failure to locate Yii2 class during an Ajax request

I've created a model class that represents a table in my database: <?php namespace app\models; use yii\db\ActiveRecord; class Pricing extends ActiveRecord { } Next, I attempted to utilize a simple PHP function in a separate fil ...

Is there a way for me to insert PHP code into a file that has been generated by PHP?

I have encountered an issue where PHP files seem to convert all PHP code into emptiness. Despite creating a PHP file, adding PHP and HTML code to it, and then closing it, the code does not appear as expected in the file. I attempted a solution which is det ...