You should be escaping each of these strings (in both snippets) with mysql_real_escape_string().

https://www.php.net/mysql-real-escape-string

The reason your two queries are behaving differently is likely because you have magic_quotes_gpc turned on (which you should know is a bad idea). This means that strings gathered from $_GET, $_POST and $_COOKIES are escaped for you (i.e., "O'Brien" -> "O\'Brien").

Once you store the data, and subsequently retrieve it again, the string you get back from the database will not be automatically escaped for you. You'll get back "O'Brien". So, you will need to pass it through mysql_real_escape_string().

Answer from awgy on Stack Overflow
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 380514 › php-mysql-insert-problem-with-single-quotes
PHP Mysql Insert Problem with single quotes.. [SOLVED] | DaniWeb
September 3, 2011 - Bye :) — cereal 1,524 Jump to Post · cereal 1,524 Nearly a Senior Poster Featured Poster 14 Years Ago · You can write: $name,$field1,$field2 without quotes. Bye :) ... $name = mysql_real_escape_string($_POST['name']); $description = mysql_real_escape_string($_POST['description']); $field1= mysql_real_escape_string($_POST['field1']); $field2= mysql_real_escape_string($_POST['field2']); $field3= mysql_real_escape_string($_POST['field3']); $field4= mysql_real_escape_string($_POST['field4']); $field5= mysql_real_escape_string($_POST['field5']); $field6= mysql_real_escape_string($_POST['field6'
🌐
CodeBee
ahmsaiful.wordpress.com › 2009 › 12 › 20 › insert-string-with-single-quote-or-double-quote-in-mysql
insert string with single quote(‘) or double quote(“) in mysql | CodeBee
December 21, 2009 - 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.
🌐
YouTube
youtube.com › dosomecode
PHP String with single quotes error solved | PHP SQL single quotes inserting error - YouTube
In this tutorial, you will learn about how to add the content properly in SQL database. When we insert the content into the database this error is common, an...
Published: July 19, 2018
Views: 1K
🌐
YouTube
youtube.com › watch
How to save single and double quotes in php and mysql - YouTube
How to save single and double quotes in php and mysqlTypically, when you try to get input from user and user enters a single or double quote, then you encoun...
Published: August 31, 2020
Find elsewhere
🌐
SitePoint
sitepoint.com › php
Single quote insert problem - PHP - SitePoint Forums | Web Development & Design Community
September 27, 2011 - Hi, can anyone help? I can’t seem to get the single quotes to insert in a (daylog)textarea. I’m using the $daylog = mysql_real_escape_string($daylog);…am I missing something? Thanks much in advance! Fagin if ($go_on == true) { if (isset($_POST['submit'])) { $member = $_SESSION['login_id']; $recurrence_state = ''; if ($recurrence = method_vars('recurrence') and $recurrence == 'permanent') { $recurrence_state = 'permanent'; } ...
Top answer
1 of 4
1

Replace your single quote(') in value to BackSlash & Single Quote (\') or two single quotes ('')

Try this:

INSERT INTO QUERY (date_time, userid, user_traits, query_sql, STATUS, description, is_scheduled_row) 
VALUES ('2016-01-06 02:39:01', '307', '0,3598,1937,13891,37746,22082,2596,2431,12850,3917,1234784,44712,14638,14418,12850,2631,25003,11428,27450,2592,23593,11441,2826,36330,32219,32351,20720,13997,2594,2467,15687', 'Select * from gl_base_schema.item where national_status_cd = ''A''', 'in queue', ' (Scheduled Query #413) Pull all items where National Status Code is ''A''', 1);

OR

INSERT INTO QUERY (date_time, userid, user_traits, query_sql, STATUS, description, is_scheduled_row) 
VALUES ('2016-01-06 02:39:01', '307', '0,3598,1937,13891,37746,22082,2596,2431,12850,3917,1234784,44712,14638,14418,12850,2631,25003,11428,27450,2592,23593,11441,2826,36330,32219,32351,20720,13997,2594,2467,15687', 'Select * from gl_base_schema.item where national_status_cd = \'A\'', 'in queue', ' (Scheduled Query #413) Pull all items where National Status Code is \'A\'', 1);
2 of 4
0
$query = "Select * from gl_base_schema.item where national_status_cd = 'A'";
$sql = "insert into query (date_time, userid, user_traits, query_sql, status, description, is_scheduled_row) values ('2016-01-06 02:39:01', '307', '0,3598,1937,13891,37746,22082,2596,2431,12850,3917,1234784,44712,14638,14418,12850,2631,25003,11428,27450,2592,23593,11441,2826,36330,32219,32351,20720,13997,2594,2467,15687', '."'".$query."'".', 'in queue', ' (Scheduled Query #413) Pull all items where National Status Code is \'A\'', 1)";
🌐
PHP
php.net › manual › en › function.addslashes.php
PHP: addslashes - Manual
Instead, just use this function: <?php function Quote($Str) // Double-quoting only { $Str=str_replace('"','\"',$Str); return '"'.$Str.'"'; } // Quote ?> Modify this easily to get a single-quoting function.
🌐
Spiceworks Community
community.spiceworks.com › programming & development
Escape single quote in sql statement w/ php - Programming & Development - Spiceworks Community
August 20, 2012 - Hello, I am using php / mysql to insert records into a table. I have an array of values (from a select statement) and i am looping through the array and running an insert statement. i am passing the value {$result[‘last’]} which contains a last name. When the last name has an apostrophe ...
Top answer
1 of 4
3

Insert string with single quote(') or double quote(") in mysql

Just Use addslashes(); in Insertion and stripslashes(); for fetch data.

$str = "Hello Friend's.. Hows you all"s.";
// Outputs: Hello Friend\'s..Hows you all\"s.
echo addslashes($str);

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 (\).

$str = "Hello Friend\'s.. Hows you all"s."; // Outputs: Hello Friend's.. Hows you all"s.
echo stripslashes($str);

Now we come to the point. If we insert string into database with single or double quote like this:

$str = “Hello Friend's.. Hows you all"s.”;
$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.

$str = “Hello Friend's.. Hows you all"s.”;
$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:

echo stripslashes($str);
2 of 4
2

You can easily avoid the whole escaping thing if you use mysqli or PDO with prepared statements. The mysql_* functions are deprecated anyway, so this would be the perfect opportunity to switch.

Your code would be something like (PDO, using your code):

$query = "insert into Tory (Content) values (:tweet)";
$stmt = $db->prepare($sql);    // $db being your PDO object
$stmt->execute(array(':tweet' => $_POST['tweet']));    // assuming you are not verifying the tweet somewhere else
🌐
Stack Overflow
stackoverflow.com › questions › 28234504 › how-to-save-single-quote-in-mysql-database-in-php
how to save single quote in mysql database in php - Stack Overflow
You should use mysqli_real_escape_string to escape special characters in a string for use in an SQL statement http://php.net/manual/en/mysqli.real-escape-string.php ... Sign up to request clarification or add additional context in comments.
🌐
TutorialsPoint
tutorialspoint.com › how-to-escape-apostrophe-in-mysql
How to escape apostrophe (') in MySQL?
July 30, 2019 - mysql> create table SingleQuotesDemo - > ( - > id int, - > name varchar(100) - > ); Query OK, 0 rows affected (1.16 sec) Following direct usage does not give the desired result for name "John?s". mysql> insert into SingleQuotesDemo values(1,'John's'); '>