Updating with MySQL can only manipulate integers and not strings

Setting up seems simple, but the number of potential causes is overwhelming for someone new to programming like me:

In JavaScript, I define and later call:

function dbUpdate(x, y, z) {
    $.ajax({
        url: 'php/dbUpdate.php',
        type: 'post',
        data: {table: x, column: y, value: z},
        success: function(html) {
        }
    });
}

dbUpdate("userbase", "currentLanguage", "en");

In php/dbUpdate.php, this data is received and processed into an UPDATE query, which has worked numerous times:

$userID = 0;

$table = ($_POST['table']);
$column = ($_POST['column']);
$value = ($_POST['value']);

mysql_query("UPDATE $table SET $column=$value WHERE id=$userID", $con);
// assume $con is defined

The issue arises when the 'z' argument in dbUpdate is an integer (e.g., var z = 70;), there are no problems. However, with a simple string (e.g., var z = "en"; as shown above), it fails to update.

I've checked the database's structure, and it's set to 'varchar', so it should be able to accept strings. But who am I to assume I understand everything fully.

I suspect it may be a basic syntax error that a beginner might miss, but I'm tired of endlessly tweaking the syntax without any success, or having it cause issues later on when used differently.

I'm hopeful that you wonderful folks can assist me in resolving this!

Answer №1

Consider safeguarding the variables:

mysqli_query("UPDATE $table SET '".$column."'='".$value."' WHERE id='".$userID.'", $con);

It's important to avoid using mysql_* functions due to security vulnerabilities (failure to sanitize $_POST values). Transition to mysqli_* or PDO promptly, as the current code you are using is outdated.

By allowing unchecked input from users into your server, you're creating major security risks that could easily be exploited (simply disabling javascript can bypass client side checks).

Answer №2

If your ID is an integer, simply include your value within quotes.

mysql_query("UPDATE $table SET $column='$value' WHERE id=$userID", $con);

Answer №3

Vlad was the one who helped me figure this out.

For anyone else looking to achieve the same goal in the future:

I needed dbUpdate.php to be versatile enough to handle both strings and integers, so following Vlad's suggestion, I implemented the following code:

$userID = 0;

$table = ($_POST['table']);
$column = ($_POST['column']);
$value = ($_POST['value']);

if (is_string($value)) {
    mysql_query("UPDATE $table SET $column='$value' WHERE id=$userID", $con);
}
else {
    mysql_query("UPDATE $table SET $column=$value WHERE id=$userID", $con);
}

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

Encountering issues with running the Laravel migrate command on a remote database

Currently, I am working on a project that utilizes the Laravel 4.2 framework. My intention is to run the command php artisan migrate, but unfortunately, upon execution, an error message appears: [PDOException] SQLSTATE[42000]: Syntax error or ac ...

You are limited to storing only up to 2 items in the localStorage

My goal is to save items in local storage as an array of objects. Initially, it works perfectly and stores the first element in local storage as needed. However, I am facing an issue where I cannot store more than one element. Below is the code block that ...

Ways to retrieve slider value when button is clicked?

I am currently working on a range-slider that has two ranges and I need to retrieve the slider value in my javascript code. Here is my approach so far: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.cs ...

The execution of Node.js on data is triggered only after the content has been successfully written

Hello, I am attempting to establish a connection to a telnet server using Node's net library. const net = require('net'); const con = new net.Socket(); con.connect(23,'10.0.0.120', () => { console.log('Telnet connected ...

What is the best way to extract the body content from a Markdown file that includes Frontmatter

How can I retrieve the content of the body from my markdown file using front matter? Currently, it is displaying as undefined. What steps should I take to fix this issue? {latest.map(({ url, frontmatter }) => ( <PostCard url={url} content={frontmat ...

Unable to stop the default action in IE for certain code

I am facing an issue on my website where the JavaScript I have implemented to prevent page reload is not working in Internet Explorer. The code functions properly in all other browsers, except IE. Here is the JavaScript code snippet that should prevent pa ...

The functionality of Jquery Ajax is limited to a single use

I'm having an issue with a page that is supposed to reload the data within a div using jQuery, but it only updates once. Here's a snippet of my code: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0 ...

The expiration period set in expireAfterSeconds doesn't seem to be functioning as expected in the time-to-live (ttl) configuration. Rows are

Can you please take a look at my code and provide some feedback? The issue I am facing is that the record is getting deleted without specifying the number of seconds. I have tried changing from createIndex to ensureIndex but it's still not working as ...

Is Ember CLI experiencing issues due to the latest Ember Data update?

Greetings! I am a beginner with Ember and recently encountered some warnings after upgrading to the latest version of Ember Data: Update: I have two identical versions of my app, one built without ember-cli and the other with ember cli. Both applications ...

Tips on accessing the v-model value with a parameter in VUE

Looking to access the v-model value using a parameter, I attempted the following code: <template> <div v-for="(item, idx) in data"> <input :id="item" :v-model="item"></input> <button @click=&q ...

loading xml data into a table partially using jquery

Currently, I am utilizing AJAX to load and parse XML data. I have constructed a table where I am inserting the data from the XML using a loop. The issue lies in the fact that only around 3000 rows are being inserted into the table even though the XML conta ...

Bootstrap Popover not displaying information after an AJAX request

I'm struggling to update the popovers contents with Ajax result in my ASP.Net MVC4 project. Using ASP.Net (MVC4): public ActionResult GetEmployeeDetails(string employeeId) { var contract = UnitOfWork.ContractRepository.ContractBu ...

Prevent parent element from changing color when child element is clicked

Check out my HTML: .imageURL:active .image_submit_div { background-color: #ccc; } .image_submit_div:active { background-color: #e6e6e6; } <div class="image_div"> <label for="id_image" class="image_submit_div"> <h3>+ add ...

Implementing a toggle function in Vue.js to add or remove a class from the body element when a

I'd like to add a toggleable class to either the body element or the root element("#app") when the button inside the header component is clicked. Header.vue : <template lang="html"> <header> <button class="navbar-toggler navbar-tog ...

Tips for avoiding background color interference with raycaster

In my current three js scene, I have a ground, sky, and various objects. I want specific objects to change color to red when the mouse hovers over them, but not all objects should do this. Currently, everything I touch turns red, which is not what I want. ...

What would be the optimal type for the second argument of the `simulate` method?

When using the simulate function, I am familiar with code like this: simulate("change", { target: { value: '7' } }); However, if my onChange function requires an object as a parameter, what should I pass in the second argument? interface myObj ...

Is it possible to have several forms triggered by the same AJAX call but produce varying results

If I have multiple PHP-generated forms and want to include a "Reply" button on each form that sends its content via ajax to another PHP page, can I use the same JavaScript code snippet for all of these elements? <form id="foo"> <label for="ba ...

Samsung S4 Android device experiencing interruption in HTML5 video playback

When using Android Webview to play html5 videos, including Youtube videos (using my own tags and Youtube embedded iFrames), I came across an issue with the Samsung Galaxy S4. The problem occurs in the following scenario: Play a video. Press 'back&ap ...

What steps can I take to resolve the TypeError in next js where I am unable to set properties of undefined for 'className'?

I've been working on a Next.js project and I've copied some code from CodePen that is used for designing product layouts on an e-commerce website. However, I'm encountering a problem with the following error message: TypeError: Cannot set pr ...

Chakra UI: How come the tooltip is appearing in the top left corner of the screen instead of directly above the element?

CreatedByModal is a unique chakra modal that incorporates tooltips. However, I am facing an issue where the tooltip appears at the top of the screen instead of directly above the icon when hovering over the icons. You can see in the image provided that the ...