Storing a multiline HTML-encoded text within an SQL database

How can I effectively store HTML code in an SQL database after encoding it with encodeURI()? Unfortunately, I am encountering multiple errors while attempting to do so.

https://i.sstatic.net/QKNYt.png

I have tried using dataType as <CLOB> and also experimented with NVARCHAR(MAX), yet the same error persists.

Below is the encoded HTML code that I want to store in SQL in string format:

%3Cp%3Ethis%20kind%20of%20text%20i'm%20storing%20into%20database%3C/p%3E%3Cpre%20class=%22code-pre%22%3Evar uri%20= %22my%20test.asp?name=st%C3%A5le&car=saab%22;%0Avar enc%20=%20encodeURI(uri);%0Avar dec%20=%20decodeURI(enc);%0Avar res%20=%20enc%20+ %22<br>%22 +%20dec;%0A%3C/pre%3E

INSERT INTO "Mytable" VALUES(
8/*ID <INTEGER>*/,
'Return matching objects from array of objects'/*QUESTION <NVARCHAR(200)>*/,
'%3Cp%3Ethis%20kind%20of%20text%20i'm%20storing%20into%20database%3C/p%3E%3C     pre%20class=%22code-pre%22%3Evar uri%20= %22my%20test.asp ? 
 name=st%C3%A5le&car=saab%22;%0Avar enc%20=%20encodeURI(uri);%0Avar 
 dec%20=%20decodeURI(enc);%0Avar res%20=%20enc%20+ %22<br>%22 
 +%20dec;%0A%3C/pre%3E'/*QUESTION_DESC <CLOB>*/,
'20170508'/*CREATED <TIMESTAMP>*/,
0/*USERID <INTEGER>*/,
1/*TAGID <INTEGER>*/
);

The command above is what I am using to insert data into the database. The string for QUESTION_DESC has been encoded, with the original string being:

<p>this kind of text i'm storing into database</p><pre class="code- 
pre">var&nbsp;uri =&nbsp;"my test.asp?name=ståle&amp;car=saab";
var&nbsp;enc = encodeURI(uri);
var&nbsp;dec = decodeURI(enc);
var&nbsp;res = enc +&nbsp;"&lt;br&gt;"&nbsp;+ dec;
</pre>

Your assistance is greatly appreciated.

Answer №1

Posting html code from middleware was a breeze at first, but things took a turn when trying to store it in the database. An error kept popping up due to some unexpected double quotes. The solution? I decided to replace those pesky double quotes with something to ignore them before sending the html code from the middleware.

The snippet of my html code destined for the database:

var htmlCodeToBeStored = 
"<p>this kind of text i'm storing into database</p><pre class="code- 
 pre">var&nbsp;uri =&nbsp;"my test.asp?name=ståle&amp;car=saab"; 
 var&nbsp;enc = encodeURI(uri);
 var&nbsp;dec = decodeURI(enc); 
 var&nbsp;res = enc +&nbsp;"&lt;br&gt;"&nbsp;+ dec;
 </pre>"

To tackle the issue, I made a simple modification by replacing the troublesome double quotes as shown below:

htmlCodeToBeStored = htmlCodeToBeStored.replace(/"/g, "\"")

Thanks to this straightforward adjustment, I successfully managed to store my answer in the database without any hiccups.

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

Could Ramda assist in enhancing pipeline/composition processes with a logging feature?

Considering implementing logging within a composed chain of functions, the following code demonstrates how it can be achieved: const f = R.compose( transformation2, doAlso(x => console.log(`id: ${x.id}`)), transformation1 ) This approach would c ...

Dividing a string in JavaScript to generate fresh arrays

I am currently dealing with the following string: "ITEM1","ITEM2","ITEM3","ITEM4","ITEM5","ITEM6","ITEM7"~100000000,1000048,0010041,1,1,1,1~100000001,1000050,,2,0,2,1~100000002,1068832,0 ...

JS: When the 'less than' operator falls short

I am facing an issue with a script that sends an AJAX request every 10 seconds (technically 11) to the user. I have created a simple countdown from 10 to 0 that repeats continuously. The countit function is triggered after each AJAX request to reset the c ...

I'm running into some issues with flexbox and I'm in need of some assistance to find

I positioned two divs next to one another, but instead of each taking up 100vw, they are both sharing 50% of the available space. Is there a solution for this issue? Thank you. Page Image import type { AppProps } from "next/app"; import "./global.cs ...

Arrange an array in JavaScript according to the value of its keys

I am looking to rearrange an array of Objects with status 'Pass' & 'Fail'. I want to move all Fail ones to the top and Pass ones to the bottom. Is there a specific array method that can help achieve this? let a = [ { name: 'x&apo ...

React: Trying to use the map function on an empty array will result in an error

I am currently facing an issue while trying to populate a shopping cart with items. Even though I have initialized the cart as an empty array, I keep encountering the following error: TypeError: cart.map is not a function ProductContext.js:34 addItemToCar ...

Steps for establishing a connection to a MongoDB database on Heroku using mongoose

Everything is running smoothly with my app locally and it can successfully connect to the local database. However, when I attempt to run it on Heroku, I encounter the following error: 2014-04-17T06:32:23.404458+00:00 app[web.1]: > <a href="/cdn-cgi ...

Troubleshooting: Angular 6 Renderer2 Issue with Generating Dynamic DOM Elements for SELECT-Option

Currently, I am attempting to dynamically create a select option using Renderer2. Unfortunately, I am facing difficulties in creating the <Select></Select> element, but I can confirm that the <options> are being successfully created. Due ...

Generate a PDF document on the user's device

Is there a way for me to trigger the print command on a PDF file without relying on Adobe PDF viewer's print button? I'm interested in using a separate event instead of the print button within the PDF viewer. Is this achievable? ...

steps to eliminate a cookie upon second click

I have successfully implemented a feature where the color of a button is stored in a cookie, so that when the button is clicked, the CSS color remains even after page refresh. However, I am struggling to figure out how to destroy the cookie after the secon ...

Tips for converting JSON object values to a string using JavaScript

Consider the following JSON object: { "id": 1, "name": "Name", "surname": "Surname", "number": "111111111" } Is there a way to transform this JSON object into a string that formats it as follows using JavaScript? Name Surname 111111111 ...

I've been attempting to develop a React application, but I consistently encounter the following error: "npm ERR! cb() function was never invoked!"

Here is the issue at hand: HP@DESKTOP-1HP83V8 MINGW64 ~/Desktop/Web-Development (master) $ npx create-react-app my-app A new React app is being created in C:\Users\HP\Desktop\Web-Development\my-app. Packages are being installed. ...

Blurring the boundary of Slick's carousel with a fade effect

Currently, I am utilizing the Slick Carousel plugin developed by Ken Wheeler. In the image provided below, you can observe that when a slide is transitioning at the edge, it appears to be abruptly cut off. I am contemplating the implementation of a "fading ...

Omit the <span> tag when exporting to XLS format

Currently, I have a functioning jQuery DataTable that utilizes the TableTools plug-in and includes <span> elements in one of the columns for each row. When clicking on the export button, my goal is to exclude or hide the <span> elements from t ...

Attempting to showcase a PDF document within a web browser

I am a newcomer to JavaScript and I'm encountering an issue with displaying a PDF file in the browser. Every time I try to do so, I keep receiving the same error message 'Cannot GET...' Despite trying various methods... router.get("/en ...

Why does the child Vuex Store Object return undefined while the parent returns correctly?

I've come across some similar inquiries, but none quite fit my specific scenario. Upon logging this.$store.state.account, the expected results are displayed {__ob__: Nt} user: Object favorites_playlist: (...) firebaseI ...

Getting information from MongoDB using Node.js and Angular

Currently, I am facing difficulty in retrieving data from MongoDB (I'm also using Mongoose) and sending it to Angular in order to populate the ng-repeat list with the retrieved data. I have managed to either display the data on a blank page directly f ...

Retrieve keys from objects by specifying partial key strings using Lodash

Can LoDash's _.filter be utilized in a scenario where you want to retrieve values based on the presence of a specific string within the keys? For instance, consider the following data: Mydata{ "banana" : "1" } If I aim to extract values containing " ...

Encountering an issue when attempting to execute a MySQL stored procedure

When I try to execute the procedure below, I encounter an error. The issue seems to be arising from casting the varchar variable to unsigned in the code snippet. Error Message : Error Details : Query : call procedureName(@examDetails) Error Code : 105 ...

Fetching data dynamically using ajax as you scroll

Currently, I am using the jQuery Tools Plugin to create an image slider. However, I am facing a challenge with loading a large number of images all at once. Since the slider is coded in JavaScript, I am not sure how to control the scroll position. I would ...