I've been following the CODECALL forums for some time now. Finally, the time has come to register and be a part of the community.
I'm a newbie PHP coder, working on my very first datebase driven application. The first step was the registration script which I finally got it working, but I have two problems that I want to share, and possibly, get some guidance on how to solve them. The first is, as the title says, the prevention of SQL injections. I understand the concept, but I have a hard time implementing it in my code, which you can see below.
The another question, which is less important until I prevent SQL injection is the registering itself. It does check if the username exists, but somehow I can store same usernames in the database, which both are different by their user_id. How can I stop this?
Anyway, here's the code. Any help is appreciated.
<?php
// Connection with databse
$con=mysql_connect ("localhost", "root", "password");
mysql_select_db ("thedatabase");
// Storing the values submitted by form
$username= strip_tags($_POST['username']);
$pass= strip_tags($_POST['password']);
$password=md5($pass);
$email= strip_tags($_POST['email']);
// Checking if the username is already in use
$queryuser=mysql_query ("SELECT * FROM users WHERE username='$username' ");
$checkuser=mysql_num_rows($queryuser);
if ($checkuser !=0)
{
echo "Sorry, ".$username." is already been taken.";
}
// A query that inserts user into databse
$insert_user=mysql_query ("INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')" );
if ($insert_user)
{
echo "Registration successful";
}
else
{
echo "Error in registration";
}
?>
Thank you in advance!


Sign In
Create Account


Back to top









