i have commented the sprintf statement to insert values in the table and used a normal insert statement which i used earlier.
also the select query is now doing its task of checking the username if it is already in the table as i have used
$selectqueryusername = "Select username from individuals where username = '$username'"; INSTEAD OF
$selectqueryusername = "Select username from individuals where username='%s'", mysql_real_escape_string($username); OR
$selectqueryemail = sprintf("Select email from individuals where email='%s'", mysql_real_escape_string($emailID));
the sprintf syntax is =
$conn = mysql_connect($hostname, $user, $passwordidb);
$insertquery = sprintf("INSERT INTO individuals (username, email, ....) VALUES ('%s', '%s',....)", mysql_real_escape_string($username, $conn), mysql_real_escape_string($email, $conn), ....);
the simple insert statement is =
$insertquery = "INSERT INTO individuals(username, email, ...) VALUES ('$username', '$email', ...)";
however what i need is the data should be safe before the insert query is executed and presently the way the sprintf is written is not doing what it is supposed to do. i have taken this idea from the following url
http://in2.php.net/mysql_real_escape_string
i have tried different combinations of the sprintf statement some dont work and for some all the values are not being inserted into the table.
following are the combinations i have tried.
1.
$insertquery = sprintf("INSERT INTO individuals (username, password....) VALUES ('%s', '%s', ...)", mysql_real_escape_string($username, $conn), mysql_real_escape_string($password, $conn), ...);
2.
$insertquery = sprintf("INSERT INTO individuals (username, password....) VALUES ('%s', '%s', ...)", mysql_real_escape_string($username), mysql_real_escape_string($password), ...);
3.
$insertquery = sprintf("INSERT INTO individuals (username, password, ...) VALUES ('%s', '%s', ...)", mysql_real_escape_string($username, $conn), mysql_real_escape_string($password, $conn), ...);
4.
$insertquery = sprintf("INSERT INTO individuals ('username', 'password', ...) VALUES ('%s', '%s', ...)", mysql_real_escape_string($username, $conn), mysql_real_escape_string($password, $conn), ...);
5.
$insertquery = sprintf("INSERT INTO individuals (username, password....) VALUES ('$username', '$password', ...)");
in case of 5 prior to the sql insert statement i have used
$username = mysql_real_escape_string($_POST["username"]); ...
i am not sure which is the right method or if there is any other way.
mainly my approach to avoiding the sql injection is
if(get_magic_quotes_gpc())
{
$username = stripslashes($POST["username"]); ...
}
else
{
$username = $POST["username"]; ...
}
$conn = mysql_connect($hostname, $user, $passwordidb);
if(!$conn)
{
}
else
{
mysql_select_db($database, $conn);
$insertqueryresult = mysql_query($insertquery);
mysql_close($conn);
}
========================================================================
i would really appreciate if anyone can help me to solve this problem, please suggest the right syntax for sprintf, i have used different combinations in sprintf = " ' ` not sure which is correct.
any help will be greatly appreciated.
waiting for reply.
thanks.