In response to a previous question I had posted,there was a response that I was opening up my database to SQL injection. The code is given below :
<?php
$firstname = stripslashes(strip_tags($_POST['firstname']));
$lastname = stripslashes(strip_tags($_POST['lastname']));
$email = stripslashes(strip_tags($_POST['email']));
$title = stripslashes(strip_tags($_POST['title']));
$organization = stripslashes(strip_tags($_POST['organization']));
$pdbHost = "localhost";
$pdbUserName = "******";
$pdbPassword = "******";
$pdbName = "db1080824_emails";
// Connect to mySQL
$conlink = mysql_connect($pdbHost, $pdbUserName, $pdbPassword);
if(!$conlink) {die('Unable to connect to '.$pdbHost);}
if (!mysql_select_db($pdbName, $conlink)){die('Cannot find database '.$pdbName);}
//SQL query
$SQL2="INSERT INTO `db1080824_emails`.`emails` (`record_id` ,`firstname`,`lastname`,`email`,`title`,`organization`)VALUES (NULL , '".$firstname."', '".$lastname."', '".$email."', '".$title."', '".$organization."')";
mysql_query($SQL2);
// Connect to Closing the connection
mysql_close($conlink);
?>
A suggestion was that I can do server side validation checks for '/^[A-Za-z0-9]/' to ensure no sql injection might happen but is that sufficient or is there best practice I should follow to ensure data sanitization ?