What is the process for replacing " with ' in my text?

Seeking assistance to run a SQL query from JavaScript that will execute in a SQL server. The query needs to locate a user in the database based on their username and password:

var str = "SELECT * FROM Users where (username = " + params.user + ") and (password = " + params.password + ")";

The parameters, params, are sent by the user in the URL:

localhost:8888/login?user="abc"&password="123"

However, the value of str ends up as:

str = SELECT * FROM Users where (username = "abc") and (password = "123")

I attempted to use string.replace to change the double quotes with single quotes, but when I print str, it still shows double quotes instead of single.

Any recommendations? It's possible that I'm not the only one who has encountered this issue...

Answer №1

Follow these steps:

let query = "SELECT * FROM Users where (username = " + params.user.replace(/"/g, "'") + ") and (password = " + params.password.replace(/"/g, "'") + ")";

This code snippet will replace the double quotes with single quotes.

Check out the live DEMO here

Answer №2

Check out this parameterized SQL example below for achieving the desired task.

let query = "SELECT * FROM employees WHERE (name = @name) AND (department = @dept)";

let sqlCommand = new SqlCommand(query, connection);

let sqlParameters = new List<SqlParameter>()
{
    new SqlParameter("@name", SqlDbType.VarChar) { Size = 50, Value = params.name },
    new SqlParameter("@dept", SqlDbType.VarChar) { size = 30, Value = params.department },
};    

sqlCommand.Parameters.AddRange(sqlParameters.ToArray());

Answer №3

Include the ' in your SELECT statement, for example:

var str = "SELECT * FROM Users where (username = '" + params.user + "') and (password = '" + params.password + "')";

Make sure to remove the double quotes from the request:

localhost:8888/login?user=abc&password=123

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

Having trouble accessing array elements in react components

When retrieving JSON data for a single student from the server in my React application, I am able to access this.state.info.Firstname but encountering difficulty accessing this.state.info.Father.Firstname. How can I access this information? This is my Rea ...

Appium with Node.js (wd) becomes unresponsive when unable to locate element

Encountering an issue while using appium with nodejs (wd) and mocha, as there is a loading view in the android app (blackbox testing & I'm not the developer) that needs to be waited for its disappearance. Attempted the following solution: wd.addPromi ...

Refresh the Kendo Auto Complete feature within a grid whenever the grid page is changed

Incorporating a kendo auto complete into the filter feature of a grid column has presented a challenge for me. I am looking to have the auto complete update based on the current page number and size whenever the page changes. Despite exploring different s ...

Flipping a string in the latest Swift update, version 4.2

iOS 11, Swift 4.2, Xcode 10 Despite following advice from various forums and search results, this supposedly functioning code is not working as expected. let str = self.label!.text let newStr = String(str?.reversed()) An error message stating 'Cann ...

Troubleshooting Issue with Filtering Nested Object Array Based on Property

At the core of my data structure lies an array of orders, each containing an array of line items. These line items, in turn, are associated with their respective categories. I am currently attempting to filter the order array based on the category ID of th ...

Is it possible to use jQuery for drag-and-drop functionality?

Currently, I am working on developing a drag-and-drop widget that consists of 3 questions and corresponding answers. The user should only be able to fill in 2 answers in any order, while the third drop area should be disabled. This third drop area can be l ...

Retrieve values from the query string (specifically from table rows and cells) for each individual line and display them in

i have some code, see: <script> $$.ready(function() { // Profile Dialog $( "#user-dialog" ).dialog({ autoOpen: false, modal: true, width: 400, open: function(){ $(this).parent().css('overflow', 'visible') ...

Searching for multiple white spaces in JavaScript to ensure that no empty data is transmitted and removing any excess spaces

I'm a little puzzled on how to tackle this issue. It seems like a common problem, but my search didn't yield any helpful results. I have an HTML textarea and input textbox that I want to validate in the following ways: Prevent users from enteri ...

Separate the navbar-brand and nav-items onto separate lines within the Bootstrap 4 navbar

I would like to arrange the navbar-brand and nav-items on separate rows. Put the navbar brand on the first row and the navigation items on the second row Existing code: <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="nav ...

Ways to showcase every resource from an API in React JS

I need help with adding code to display products along with their images from an API in my existing code. Can someone assist me with this? import React, {useState, useEffect} from 'react' import "bootstrap/dist/css/bootstrap.min.css" im ...

Creating a Javascript countdown timer that does not involve displaying the

I stumbled upon this code on a website, but there's one tweak I'd like to make. Unfortunately, I can't seem to figure it out myself, so I'm reaching out for some help. What I want to achieve is removing the year from the date so that th ...

Unable to disable webpack HMR

I have set up my project using express version 4.14.0, webpack version 1.14.0 with babel and its presets. I obtained this sample webpack configuration from a reliable source: var path = require('path'); module.exports = { entry: './main. ...

Encountering an undefined json array when making an AJAX request

There have been numerous questions on this topic, but none of the specific solutions seemed to apply to my situation. So, I apologize if this is a duplicate query. I am currently working on fetching data from an SQL database using a PHP file that passes t ...

Validation of textfields using React.js

Currently, I am working on implementing a validation feature in ReactJS. Specifically, I have a field named "name" and my requirement is that every time a name is entered, it must be equal to or greater than 2 characters. The validation works fine when t ...

Custom Script for Single Tumblr Entry

While I know it's possible to edit the HTML/AngularJS for the overall posts on a Tumblr blog homepage, I'm wondering if there is a way to insert a custom <script>...</script> into individual posts. I have some specific JavaScript task ...

Include a personalized header in $http

In my factory, I have multiple functions that follow this structure: doFunction: function () { return $http({ method: 'POST', url: 'https://localhost:8000/test', data: {}, headers: { "My_Header" ...

Storing an image or video file to the disk of a NodeJS server and creating a reference in MongoDB

Seeking advice on the optimal method to store an image or video file on a NodeJS server and link it to MongoDB. Additionally, I need to enable streaming for the videos. Any recommendations would be greatly appreciated. Thank you! ...

Creating a text using the properties in a UUID instance

I have a function that successfully prints a UUID as shown in the code below: void printUUID(uuid_t u) { int i; printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid, u.time_hi_and_version, u.clock_seq_hi_and_reserved, u.clock_se ...

Retrieve the URL of an image located within an <li> tag within a jQuery selector item array

I am currently using a jQuery slider plugin to display images, and I am attempting to retrieve the URL of the current image when a button is clicked. The slider functions by showcasing all the image slides as list items in a continuous horizontal row. It ...

The <br/> tag is not functioning properly when retrieving text from a MySQL query

Currently, I am involved in a project that involves an A.I pushing text onto a MySQL database. Our goal is to display the ongoing writing process on a webpage. We are still actively working on this. We are retrieving the data and regularly checking the da ...