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().
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().
For anyone finding this solution in 2015 and moving forward...
The mysql_real_escape_string() function is deprecated as of PHP 5.5.0.
See: php.net
Warning
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_real_escape_string()
PDO::quote()
you need to use mysql_real_escape_string on the value, which you should be doing anyway. That should properly escape your value for insertion.
$name = mysql_real_escape_string($e['NAME']);
$brand = mysql_real_escape_string($e['BRAND']);
$category = mysql_real_escape_string($e['CATEGORY']);
$query = "INSERT INTO table2 (brand, name, category) VALUES ('$brand', '$name', '$category')";
Use mysql_real_escape_string
$schname = addslashes($_GET['schname']);
Use variable with addslashes function
Need to wrap the variable in quotes.
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$schname = $mysqli->real_escape_string($_GET['schname']);
$sql= "INSERT INTO `school` (id, schname, place) VALUES (' ', '$schname', 'place')";
$mysqli->query($sql);
Ref. http://php.net/manual/en/mysqli.real-escape-string.php
Put quite simply:
SELECT 'This is Ashok''s Pen.';
So inside the string, replace each single quote with two of them.
Or:
SELECT 'This is Ashok\'s Pen.'
Escape it =)
' is the escape character. So your string should be:
This is Ashok''s Pen
If you are using some front-end code, you need to do a string replace before sending the data to the stored procedure.
For example, in C# you can do
value = value.Replace("'", "''");
and then pass value to the stored procedure.
prepare the data by replacing one ' with two '', before composing the query:
while($row = mysql_fetch_array($result_query2)) {
$n = mysql_real_escape_string($row["name"]);
$s = mysql_real_escape_string($row["sku"]);
$q = mysql_real_escape_string($row["sku"]);
// $n = html_entity_decode($row["qty_ordered"]);
$result_str_product .= "('$n','$s','$q'),";
}
// remember_to_remove_final_stray_comma($result_str_product);
print( $result_str_product ); // just to see what's been made
For Oracle, you could replace the single quotes with two single quotes when you query:
$query2 = "SELECT REPLACE(sfoi.name,'''','''''') name, sfoi.sku, sfoi.qty_ordered
FROM sales_flat_order sfo JOIN sales_flat_order_item sfoi
ON sfoi.order_id = sfo.entity_id
WHERE sfo.increment_id = 100000473";
Then the rest of your code should work as is.
In Oracle, two consecutive single quotes represent a single quote in a string literal.
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);
$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)";
You are approaching this problem in a wrong way: rather than preparing the string to be "pasted" into SQL Server's query, parameterize your SQL, and pass the string as a parameter. This way you wouldn't have to escape it at all, and the number of quotes or other special characters wouldn't matter either:
$sql = "INSERT INTO MyTable(id,name) VALUES (?,?)"
$params = array($someId, $name)
$sql_srv_query($db_conn, $sql, $params);
Using prepared statements is the best way. If you insist on a regex way, you can double single quotes with preg_replace so that there is an even number of consequent single quotes:
''|(')
And replace with ''. See demo
Sample PHP code:
$re = "/''|(')/";
$str = "You're Doing It Wrong!!,'''Mike Walsh'',Intermediate";
$subst = "''";
$result = preg_replace($re, $subst, $str);
Output:
You''re Doing It Wrong!!,''''Mike Walsh'',Intermediate
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);
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