How to insert single quote in mysql using php

I have a perplexing issue that I can't seem to comprehend...

I have two SQL statements:

  • The first enters information from a form into the database.
  • The second takes data from the database entered above, sends an email, and then logs the details of the transaction

The problem is that it appears that a single quote is triggering a MySQL error on the second entry only! The first instance works without issue, but the second instance triggers the mysql_error().

Does the data from a form get handled differently from the data captured in a form?

Query 1 - This works without issue (and without escaping the single quote)

$result = mysql_query("INSERT INTO job_log
(order_id, supplier_id, category_id, service_id, qty_ordered, customer_id, user_id, salesperson_ref, booking_ref, booking_name, address, suburb, postcode, state_id, region_id, email, phone, phone2, mobile, delivery_date, stock_taken, special_instructions, cost_price, cost_price_gst, sell_price, sell_price_gst, ext_sell_price, retail_customer, created, modified, log_status_id)
VALUES
('$order_id', '$supplier_id', '$category_id', '{$value['id']}', '{$value['qty']}', '$customer_id', '$user_id', '$salesperson_ref', '$booking_ref', '$booking_name', '$address', '$suburb', '$postcode', '$state_id', '$region_id', '$email', '$phone', '$phone2', '$mobile', STR_TO_DATE('$delivery_date', '%d/%m/%Y'), '$stock_taken', '$special_instructions', '$cost_price', '$cost_price_gst', '$sell_price', '$sell_price_gst', '$ext_sell_price', '$retail_customer', '".date('Y-m-d H:i:s', time())."', '".date('Y-m-d H:i:s', time())."', '1')");

Query 2 - This fails when entering a name with a single quote (for example, O'Brien)

$query = mysql_query("INSERT INTO message_log
(order_id, timestamp, message_type, email_from, supplier_id, primary_contact, secondary_contact, subject, message_content, status)
VALUES
('$order_id', '".date('Y-m-d H:i:s', time())."', '$email', '$from', '$row->supplier_id', '$row->primary_email' ,'$row->secondary_email', '$subject', '$message_content', '1')");

In developing web application, we write insert query for inserting data into database. Hence i use mysql query and PHP functions for inserting string with single quote(‘) or double quote.

let we know two useful PHP function :

1.  addslashes — Quote string with slashes. Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).

<?php
$str
= "Is your name O'reilly?";
// Outputs: Is your name O\'reilly?
echo addslashes($str);
?>

2.stripslashes —  Un-quote string quoted with addslashes(). Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).

<?php
$str
= "Is your name O\'reilly?";

// Outputs: Is your name O’reilly?
echo stripslashes($str);
?>

Now come to the point…..if we insert string into database with single or double quote like this :
<?php
$str = “Is your name O’reilly?”;
$query = “INSERT INTO tbl (description) VALUES ( ‘$str’)”;
?>
This will occur error.

but if we use addslashes($str) function like below and then insert into database, then no error will be occurred.
<?php
$str = “Is your name O’reilly?”;
$desc_str = addslashes($str);
$query = “INSERT INTO tbl (description) VALUES ( ‘$desc_str’)”;
?>

similarly we can use stripslashes($str) to print that table field value like this :
<?php
echo stripslashes($str);
?>

cheers 🙂

Inserting strings with single quotes in MySQL and PHP can be a bit tricky. The reason for this is that the single quote is used as a delimiter for strings in both MySQL and PHP. This can cause problems when trying to insert a string with a single quote in it.
One way to get around this problem is to use the backslash character to escape the single quote. This will tell MySQL and PHP to treat the single quote as a regular character and not as a delimiter.
Another way to insert strings with single quotes is to use double quotes instead. This will also work in most cases, but you may run into problems if the string you are trying to insert contains double quotes.
If you are having problems inserting strings with single quotes, you may want to try one of these methods.

A quotation mark must contain bytes, which can be added by ASCII, NULL, or Control+Z (if any). Backslashes must be enclosed in a one, two, or three-character character array with each quotation mark. SQL statements are used in MySQLdrive to return an appropriately escaped result using the QUOTE function. Single-quoted strings must include a statement in the first line before the one that follows, and it must not be an empty space after the one that follows. There should never be an end to double quotation when two words are combined or written precisely. When using two quotation marks, you can only use one string.

If you want to cancel out the following character if a string contains both a contraction and a quotation, you can use the backslash ” to specify only single quotes and double quotes.

MySQLi_real_escape_string() should be used to insert special characters into a database in PHP. A MySQLi_real_escape_string() function is used to retrieve users’ special characters from the form fields. The following parameters must be set in order to escape the strings and database connection.

In PHP, single and double quotes define strings with identifier such as $b $c $d, whereas single quotes define constant strings such as ‘a’,’my name’, ‘abc xyz’, and double quotes define string It makes no difference whether string is echo $a or echo $s; this is true whether the string is echo $a or echo $s.

A single quotation is provided below. String lengths can be specified in a variety of ways, with single quotes (the character ‘). To specify the literal meaning of a single quote, use the letter [*].

How Do I Insert A Quote In Mysql?

How to insert single quote in mysql using php
Credit: quotes-ops.blogspot.com

To insert a quote in MySQL, you need to use the single quotation marks around the text. For example:
INSERT INTO table_name (column_name) VALUES (‘Text goes here’);

When using the double, use (**’) instead of single if you’re citing a single quote. Multiple quotes can be wrapped inside a single quote to make their appearance more appealing. You can get a quote from Oracle if you use only one key.

You can use the escape character * (backslash) to get out of a single sentence in Python. When you want to show the value O’Reilly, you can use “‘O’Reilly.”

How To Insert A Quote In Mysql

In MySQL, the QUOTE() function is used to type quotes. When the function returns a string enclosed by a single quotation mark, it will return a string enclosed by a single quotation mark with backslash (), single quote (‘), ASCII NULL, and Control Z preceded by a backslash. To escape a quote in MySQL, use the backslash character followed by the quote character.

Can You Use Single Quotes In Php?

How to insert single quote in mysql using php
Credit: The DEV Community

When defining a string in PHP programming, a single or double quote is used. However, there are numerous distinctions between these two types of materials. Strings that are quoted are the simplest to define. When you want the string to be exactly as written, you can use it.

Ben Says Goodbye To Jessie

“Jessie said, ‘I love you,'” Ben explained.
The quotation marks are present in this scene because Ben is speaking. Because Ben is quoting someone else, Jessie, we use single quotation marks to indicate what he is quoting.

How to insert single quote in mysql using php
Credit: ntr-blog41.blogspot.com

In MySQL, you can insert a single quote by using two single quotes in a row. For example:
INSERT INTO table_name (column_name)
VALUES (‘John”s book’);
This would insert the value “John’s book” into the column_name column.

Php Add Quotes To String

In PHP, adding quotes to a string can be done in a number of ways. The most common way is to use the single quote (‘) or double quote (“) characters around the string. For example:
$string = ‘this is a string’;
$string = “this is a string”;
Another way to add quotes to a string is to use the backslash (\) character before the quote character. For example:
$string = \’this is a string\’;
$string = \”this is a string\”;
Adding quotes to a string can also be done using the heredoc syntax. This is generally used when you want to add quotes to a string that contains multiple lines. For example:
$string = << this is a string
EOT;

The ?: Operato

In PHP, the?: operator performs a series of comparisons between three expressions. If all three operands are equal but the left-hand operand is less than the right-hand operand, and the left-hand operand is greater than the right-hand operand, the value is greater than 0; otherwise, the value is less than or equal to 0.

How To Remove Single Quotes From String In Mysql

It is simple to remove single quotes, double quotes, apostrophes, backticks, and other special characters with the addition of a backslash before each character.

How do I select multiple quotes in oracle? Oracle’s single quotes can be avoided by using this method. SQL is the point at which you tell the database, “Hey, this character is in my string, don’t treat it like one of your own.” Use the backslash character to quickly move from one symbol to the next. SQL statements use escape sequences to notify the driver that the escaped portion of a string should be treated differently. Because single quotes allow you to specify almost any character, they do not attempt to parse escape codes. You should consider using a single quotation mark in a T-SQL or simple query in SQL Server. To produce a single quote, the char (39) function will insert an appended quotation. I would love to know which method do you personally use when you have to escape single quotes?

How To Add Single Quote In Sql Query String

If you want to add a single quote in a sql query string, you can do so by using two single quotes next to each other. For example, if you wanted to query for the name O’Reilly, you would use the following string:
SELECT * FROM table WHERE name = ‘O”Reilly’

Apostrophe Single Quote

Multiple quotes in written U.S. English function as a quotation, which indicates a quotation within a quotation. In contrast, apostrophes are used to indicate possessiveness or omission. In this context, the apostrophe will not change; however, this is the most important point in this discussion.

Smart quotes, as the name suggests, can be found in a variety of shapes based on whether they are opening () or closing (). All-purpose straight quotes were the most common type of quotation mark on old-fashioned typewriters, also known as inch marks. This feature detects adjacent spaces and determines whether the opening or closing quotes are prompted. apostrophes are typically found in the middle of a word, so auto-correction ensures that they are always present at the close of a quotation mark. A smart quote can go awry. If you want to use an apostrophe in the first place, you must first learn how to use it (option-shift right bracket?) or outsmart your smart quotes.

How Do You Type A Single Apostrophe?

You can make a single quotation mark by pressing the apostrophe/quotation mark key to the left of ENTER in most computers. Most computers use the double quotation mark technique by holding down SHIFT and pressing the apostrophe/quotation mark key to the left of ENTER.

Apostrophes: The Three Rules

The three rules for apostrophe usage are as follows.

Where Is Apostrophe And Single Quote On Keyboard?

When used next to the Enter key on the United States keyboard, a single quote is commonly referred to as an apostrophe, but it can also be used as a punctuation symbol.

Smart Quotes: How To Use Them In Your Word Document

Simply click on the “Straight quotes” button to add single quotes to a word document, and then click “smart quotes” to select an option that will automatically correct the quote. When you select the “smart quotes” button, the apostrophe will be inserted after the quotation mark as long as you specify the “straight quotes” option.

Should I Use Single Or Double Quotation Marks?

Single quotes were typically preferred by British people over double quotes in the past, but double quotes are now more common; in the past, double quotes were preferred in the United States by those who used them.

Why Do British People Use Single Quotation Marks?

Quotations and conversations are frequently referred to in the United Kingdom with a single quotation mark. It is because British people use inverted commas more frequently than American people do. When speaking American English, double quotation marks are commonly used, such as “What time will he arrive?” She inquired as to what she meant. When it comes to quotation marks, British people tend to use them because they appear to look more professional.

How do I insert a quote in MySQL?

QUOTE() : This function in MySQL is used to return a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotation marks and with each instance of backslash (\), single quote ('), ASCII NULL, and Control+Z preceded by a backslash.

Can PHP use single quotes?

Single or double quotes in PHP programming are used to define a string. But, there are lots of differences between these two. Single-quoted Strings: It is the easiest way to define a string. You can use it when you want the string to be exactly as it is written.

How do I print a single quote in MySQL?

We can also put the character \ to indicate a quote in MySQL..
Using the Character ' Within a Word With '' to Escape Single Quote in MySQL..
Using the Character " Within a Word With "" to Escape Double Quote in MySQL..
Using the \ Character to Escape Double Quote in MySQL..

How do you put quotation marks in PHP?

Escape Quotation Marks in PHP.
Use the Backslash \ Before the Quotation to Escape the Quotation Marks..
Use the Heredoc Syntax <<< to Escape the Quotation Marks From a String in PHP..
Use the Single Quotes or the Double Quotes Alternately to Escape the Quotation Marks in PHP..