there are many way to deal with MySQL / SQL Injection
1. Filter : sanitize input or other word we can say make clean input from the bad word and blacklisted text , and make it safe.
2. Escaping :Escape the special character( like "(double quote) '(single Quote) ) which actually responsible for breaking SQL statement , special character Escaping technique depend on the SQL server Type.
3. Encoding : Encode the input value to some other safe Format.
Suppose if you do base64 encoding for all input . never you see SQL injection (it just example not preferred way )
4. Prepared Statement : in place of passing value direct to the SQL statement . pass it through by Api or function which take input value as argument and append it to the SQL safely.
Suppose . PDO - PHP Data Objects or Mysqli ((MySQL Improved Extension) is wrapper which can help to pass input value by Prepared Statement.
Mysqli Code :
$db = new mysqli($db_host, $db_user, $db_pass, $db_name, $db_port);
$stmt = $db->prepare('insert into Product(name, description) values (?, ?)');
$stmt->bind_param('%s', $_REQUEST['product_name'], $_REQUEST['product_description']);
$stmt->execute();
$stmt->close();
$db->close();
PDO code ::
try {
$db= new PDO($dsn, $db_user, $db_pass);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sth = $db->prepare(('insert into Product(name, description) values (?, ?)');
$sth->execute(array($_REQUEST['product_name'], $_REQUEST['product_description']));