Possible Duplicate:
Best way to stop SQL Injection in PHP
my question is how can I make register page more secure? On register.html the user adds some data and if in register.php the username exists in the database it tells him to use another username.
Is it poor against SQL Injection ? In the other hand how can I ensure that users will use only alphanumeric chars?
register.php
$sql ="SELECT * FROM $table_name WHERE username= '$_POST[username]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
//get the number of rows in the result set
$num = mysql_num_rows($result);
//checks it see if that username already exists
if ($num != 0){
echo "<P>Sorry, that username already exists.</P>";
echo "<P><a href=\"#\" onClick=\"history.go(-1)\">Try Another Username.</a></p>";
exit;
}else{
$sql = "INSERT INTO $table_name VALUES
('$_POST[firstname]', '$_POST[lastname]', '$_POST[username]', password('$_POST[password]'), 'Users', '', '', '$pchange',
'$_POST[email]', '$default_url', '$verify', '')";