-1

I am trying to insert a Paragraph (which contains a single quote in middle) into database as below.

$rule="**Mobile's** are not allowed into the room...................soon";


mysql_query("insert into table_name (rule) values('$rule')");

While executing the query that paragraph is not inserting. And i have directly tried in the Mysql using SQL option. There it shown error.

Any suggestions..?

Thanks

Sree ram
  • 369
  • 3
  • 14

4 Answers4

1

Some questions to make you think:

  1. What do double quotes (") mean in PHP?

  2. What are their effect on a string which contains a variable?

  3. What is "input sanitation"?

  4. What is "character escaping"?

  5. What is PDO?

Also, and most importantly:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
STT LCU
  • 4,348
  • 4
  • 29
  • 47
1

The best way to handle such thing is to use PDO extension or MySQLi. They are designed to prevent from SQL Injection.

example of using PDO.

$rule = "**Mobile's** are not allowed into the room...................soon";
$stmt = $pdo->prepare("insert into table_name (rule) values(:rule)");
$stmt->execute(array(':rule' => $rule));

best link that I can give that explains how bad is the query the breaks when a value contains single quotes:

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

use

$rule = mysql_real_escape_string("**Mobile's** are not allowed into the room...................soon");
Orangepill
  • 24,500
  • 3
  • 42
  • 63
0

You may use mysql_real_escape_string() before sending values to Mysql database table. please check this link http://php.net/manual/en/function.mysql-real-escape-string.php

$rule="**Mobile's** are not allowed into the room...................soon";
$rule1=mysql_real_escape_string($rule)
mysql_query("insert into table_name (rule) values('$rule1')");
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41