-2

I have written a query

mysql_query("insert into db_manufacturer(mfgName) values('$manufacturer')"

when $manufacturer = ram's or sam's etc then it is giving sql error, how to overcome this error. If I am using "" then in the db inserting the variable not the variable value.

John Woo
  • 258,903
  • 69
  • 498
  • 492
Bidyut
  • 539
  • 2
  • 8
  • 17
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://stackoverflow.com/a/14110189/1723893). – NullPoiиteя Jan 14 '13 at 07:09

5 Answers5

2
 mysql_query("insert into db_manufacturer(mfgName) values('$manufacturer')")

and when $manufacturer is "ram's" it will be like

 mysql_query("insert into db_manufacturer(mfgName) values('ram's')")

This is broken as the value closing delimiter appears twice; that is, there are an odd number of single quotemarks.

now solution

mysql_query("
  INSERT INTO db_manufacturer
    (mfgName)
  VALUES
    ('".mysql_real_escape_string($manufacturer)."')
");

A good, easy, scalable and safe approach is to use parameterized queries. The parameters for such queries are called bind variables.

Good Read

  1. How to prevent SQL injection in PHP?
  2. Are PDO prepared statements sufficient to prevent SQL injection?

Note

  1. The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So use either PDO or MySQLi

Good read

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
  2. PDO Tutorial for MySQL Developers
  3. Pdo Tutorial For Beginners
Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1
$manufacturer = mysql_real_escape_string($manufacturer);
mysql_query("insert into db_manufacturer(mfgName) values('$manufacturer')";
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1
$manufacturer = mysql_real_escape_string($manufacturer);
mysql_query("insert into db_manufacturer(mfgName) values('$manufacturer')"

but i Recommend to use either PDO or MySQLI.

Here's a great article:

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

Use mysql_real_escape_string which escapes special characters in a string for use in an SQL statement

$manufacturer = mysql_real_escape_string($manufacturer);

More details:

http://php.net/manual/en/function.mysql-real-escape-string.php

J.K.A.
  • 7,272
  • 25
  • 94
  • 163
0

It is deprecated but you are looking for mysql_real_escape_string.

but forget mysql_ and learn PDO, it is much safer.

Zevi Sternlicht
  • 5,399
  • 19
  • 31