Hi, what i'm trying to do is set up a login page for my web site. Well obviously before users can login the must be able to register. So I have started a simple registration page to test my PHP skills (which aren't good, because i'm a newbie) which only accepts a username and password. when they submit the form, I have a .php page with a script that connects to a mysql server, then selects the appropriate db, then checks to see if the user name is already in the database (someone else has already registered under that name), and if the user name is already taken tells them to pick another one. and if it has not been then inserts it into the db. Here is my code:
<?php
// ok retrieve the variables from POST
$user_n = $POST['user_name'];
$pass = $POST['password'];
//connect to the db server
$conn = mysql_connect('localhost', '', '');
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
//successfully connected to server
// make victorb17 the current db
$selected_db = mysql_select_db('victorb17', $conn);
if (!$selected_db)
{
die ('Can\'t use database! : ' . mysql_error());
}
//connected to database
// Now check to see if there is someone with that user name already in the database
$check_name = "select * from user";
$result = mysql_query($check_name);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result))
{
if ($row["user_name"] == $user_n)
{
echo "your in the db";
break;
}
if ($row == false && $row["user_name"] != $user_n)
{
echo "your not in the db";
// they do not have a user name already in use, so insert theirs
$insert_user= "insert into user (user_name, password) values('$user_n', '$pass')";
$result = mysql_query($insert_user);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
// it was sucessfully inserted, do something else here
}
}
mysql_close($conn);
?>
it gives this error: Invalid query: Unknown MySQL error
I know what is wrong, but can't figure out how to fix it. the variables: $user_n and $pass aren't in the while loops scope. how do I make it so they are? Thanks for any help!