ive been working on my login script, and ive come to a halt. i need some help with writing somthing that will check if the name has already been used

like

if ( $current_Username = $Database_username)
echo "sorry user name taken";

else
{
continue with rest of functions
}

where current_username = $_POST[username] (from the form)
and
Database_username = "SELECT * FROM reg_info WHERE name = '$current_Username')

ive tried things along those lines, but i always get errors. Does anyone know how to correctly do it??

thanks for any help that anyone can offer :)

Recommended Answers

All 11 Replies

If you don't post the errors and the code we can't help. There are too may things that could possibly hose your script up.

so does that mean that im heading in the right direction with my code?

No, that means I didn't look at it because you didn't post the errors.

commented: was rude when i was just asking for help. all i wanted was some guidance, not a smart allecky answer. -2

i was asking for you to look for errors, i wanted to know if that was on the right track.

I understand that Killer_Typo. PHP and MySQL both spit out error messages when you have an error. That makes debugging buggy code easier. That's why I showed you how to echo a mysql error after you try running a query. PHP errors stop the page and tell you which line you have a problem on. AT this point, I don't know if you have a PHP problem, a SQL logic problem, a problem with your form, or a SQL statement problem, or other.

Although I could read your code and likely debug it from just reading it, it would take ME, the guy who is attempting to help you with your code problem, one tenth the time to debug your code if I knew exactly what errors where being thrown. It would take YOU the same amount of time to copy and paste the error onto the page and post it as it does to type "I have an error". If you really don't want to explain the error, why would I want to try and debug it for you?

lol meant to say wasnt asking for you to look for errors, but ill post the code in a minute when i get back up to my computer (downstairs right now). im sorry if i have to double post, but it wont let me edit my posts after i finish posting them.

EDIT:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Register!</title>
</head>
<body>
<table bordercolor="000000" border="1" cellpadding="0" cellspacing="0">
 <tr bgcolor="#C8D6E0">
  <td colspan="2" align="center" height="180">
  This will be the location of any banners and such
  </td>
 </tr>
 <tr>
  <td  height="800" width="150"  bgcolor="C8D6E0">
  This will Contain Information pertaining to the site (IE SITE NAV)
  </td>
  <td valign="top" width="800" bgcolor="#70899f">
  <form method="post" action="new_user.php">
  <input type="hidden"  name="id" id="id" value="null">
   <table width="800" border="0" cellpadding="0" cellspacing="0">
	<tr>
	 <td colspan="2" align="center" bgcolor="#535d6f">
	  <font size="+1" color="ffffff">
	   <p align="left"><b>Registration Info:</b>
	  <font size="-">
	   &nbsp;&nbsp;All feilds required except those marked with an *
	  </font></p>
	  </font>
	 </td>
	</tr>
	<tr>
	 <td align="left" bgcolor="70899f">
	  <font color="#ffffff">
	   <p><b>User Name:</b></p>
	  </font>
	 </td>
	 <td align="left" bgcolor="70889f">
	  <input type="text" name="usr_name" id="usr_name">
	 </td>
	</tr>
	<tr>
	 <td align="left" bgcolor="70899f">
	  <font color="ffffff">
		<b>Password:</b>
	   </font>
	  <font size="-1" color="ffffff">passwords are case sensative:</font>
	 </td>
	 <td align="left" bgcolor="70889f">
	  <input type ="password" name="usr_pass" id="usr_pass">
	 </td>
	</tr>
	<tr> 
	 <td align="left" bgcolor="70899f">
	  <font color="ffffff">
		<b>Confirm Password:</b>
	  <font size="-1" color="ffffff">passwords are case sensative:</font>
	  </font>
	 </td>
	 <td align="left" bgcolor="70889f">
	  <input type ="password" name="con_usr_pass" id="con_usr_pass">
	 </td>
	</tr>
	<tr>
	 <td align="left" bgcolor="70899f">
	  <font color="#ffffff">
	   <p><b>E-Mail:</b></p>
	  </font>
	 </td>
	 <td align="left" bgcolor="70889f">
	  <input type="text" name="email" id="email">
	 </td>
	</tr>
	<tr>
	 <td align="left" bgcolor="70899f">
	  <font color="#ffffff">
	   <p><b>Confirm E-Mail:</b></p>
	  </font>
	 </td>
	 <td align="left" bgcolor="70889f">
	  <input type="text" name="con_email" id="con_email">
	 </td>
	</tr>
	<tr>
	 <td align="left" bgcolor="70889f">
	  <font color="#ffffff">
	   <p><b>Gender:</b></p>
	  </font>
	 </td>
	 <td align="left" bgcolor="70889f">
	  <select name="gender" id="gender">
	   <option id="unknown">Unknown</option>
	   <option id="male">Male</option>
	   <option id="female">Female</option>
	  </select> 
	 </td>
	</tr>
	<tr>
	 <td align="left" bgcolor="70899f">
	  <font color="#ffffff">
	   <p><b>DOB:</b></p>
	  </font>
	 </td>
	 <td align="left" bgcolor="70889f">
	 <select name="month" id="month">
	  <option id="1">Jan</option>
	  <option id="2">Feb</option>
	  <option id="3">Mar</option>
	  <option id="4">Apr</option>
	  <option id="5">May</option>
	  <option id="6">Jun</option>
	  <option id="7">Jul</option>
	  <option id="8">Aug</option>
	  <option id="9">Sep</option>
	  <option id="10">Oct</option>
	  <option id="11">Nov</option>
	  <option id="12">Dec</option>
	 </select>
	 <select name="day" id="day">
	  <option id="1">1</option>
	  <option id="2">2</option>
	  <option id="3">3</option>
	  <option id="4">4</option>
	  <option id="5">5</option>
	  <option id="6">6</option>
	  <option id="7">7</option>
	  <option id="8">8</option>
	  <option id="9">9</option>
	  <option id="10">10</option>
	  <option id="11">11</option>
	  <option id="12">12</option>
	  <option id="13">13</option>
	  <option id="14">14</option>
	  <option id="15">15</option>
	  <option id="16">16</option>
	  <option id="17">17</option>
	  <option id="18">18</option>
	  <option id="19">19</option>
	  <option id="20">20</option>
	  <option id="21">21</option>
	  <option id="22">22</option>
	  <option id="23">23</option>
	  <option id="24">24</option>
	  <option id="25">25</option>
	  <option id="26">26</option>
	  <option id="27">27</option>
	  <option id="28">28</option>
	  <option id="29">29</option>
	  <option id="30">30</option>
	  <option id="31">31</option>
	 </select>
	 <input type="text" name="year" id="year" maxlength="4" size="4">
	 </td>
	</tr>
	<tr>
	 <td align="left" bgcolor="70889f">
	  <font color="#ffffff">
	   <p><b>Alternate E-Mail:<i>*</i></b></p>
	  </font>
	 </td>
	 <td align="left" bgcolor="70889f">
	  <input type="text" name="alt_email" id="alt_email">
	 </td>
	</tr>
	<tr>
	 <td align="left" valign="top" bgcolor="70889f">
	  <font color="ffffff">
	   <p><b>Interests:*</b></p>
	  </font>
	 </td>
	 <td align="left" bgcolor="70889f">
	  <textarea rows="5" cols="20" name="interests" id="interests">200 character max</textarea>
	 </td>
	</tr>
	<tr>
	 <td align="left" valign="top" bgcolor="70889f">
	  <font color="ffffff">
	   <p><b>Hobbies:*</b></p>
	  </font>
	 </td>
	 <td align="left" bgcolor="70889f">
	  <textarea rows="5" cols="20" name="hobbies" id="hobbies">200 charcater max</textarea>
	 </td>
	</tr>
	<tr>
	 <td colspan="2" align="center" valign="top" bgcolor="70889f">
	  <input type="submit"> 
	 </td>
	</tr>
   </table>
  </form>
  </td>
  </tr>
</table>
</body>
</html>

heres the PHP that submits into the database, but is supposed to first check to see if the name has already or not already been used. ive left that part in the comment.

<?php
$DBhost = "localhost";//location of mySQL on server/site
$DBuser = "michael";//User name for logging onto mySQL
$DBpass = "";//Password for logging onto mySQL
$DBName = "registration";//Name of the databse for logging into
$Table = "reg_info";//Name of the Table to be used steps to create are included
$usr_name = "$_POST[usr_name]";//Name that the person gave on the form
$usr_pass = "$_POST[usr_pass]";//Password use uses
$con_usr_pass = "$_POST[con_usr_pass]";//confirmation of the password
$email = "$_POST[email]";//users email
$con_email = "$_POST[con_email]";//confirmation of the email
$gender = "$_POST[gender]";//their gender
$month = "$_POST[month]";//month they were born
$day = "$_post[day]";//day they were born
$year = "$_POST[year]";//year they were born
$alt_email = "$_POST[alt_email]";//any alternate emails they might use
$interests = "$_POST[interests]";//their interests
$hobbies = "$_POST[hobbies]";//their hobbies
	
	 mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");  //connecting to the database using the variable set
	 @mysql_select_db("$DBName") or die("Unable to select database $DBName");		 //at connection to the databse select DBNAME (phpforms) or tell that it couldnt connect
	 $sqlquery = "SEARCH * FROM $Table WHERE usr_name = '$_POST[usr_name]'";
	 $results = mysql_query($sqlquery);
	 if ($results = $usr_name) 
	  //(!$results)
	   echo "sorry that user name has been taken! please use your browsers back button and try a different one!"; 
		//echo (mysql_error());
	 else
	 {
	  mysql_close();
	if ($usr_pass != $con_usr_pass)
	  echo "sorry the passwords to not match, please use your browsers back button to fix this error";
	else
	 {
	  if ($email != $con_email)
	   echo "sorry the Emails you provided do not match, please use your browsers back button to fix this error";
	  else
	  {
	 mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");  //connecting to the database using the variable set
	 @mysql_select_db("$DBName") or die("Unable to select database $DBName");		 //at connection to the databse select DBNAME (phpforms) or tell that it couldnt connect
	 $sqlquery = "INSERT INTO $Table VALUES('$id','$usr_name','$usr_pass','$email','$gender','$month','$_POST[day]','$year','$alt_email','$interests','$hobbies')";
	   
	   $results = mysql_query($sqlquery); //query the results
	   mysql_close();
   
	 
		echo "Information has been submitted into the database!";
	  }  
	 }
	}
?>

i dont get any errors, i always get the if in the first if statment, saying that the username is taken when in fact its not. ive tried swaping the Search for Select in the mysql part.

If you are doing a comparison you need to use "==" instead of "="

if ($results = $usr_name)

is always true.

another problem solved, thank sir!

NOOOOO spoke too soon. now it submits it, even if the name is in there twice. maybe my SQL formatting is off? maybe?

lol i do have an error in my syntax, now its about finding what it is and the correct syntax to use. to the mysql manual!

not to be a jerk... but I am trying to help you, and I have helped you solve two programming problems since yesterday, and I see in my control panel that you gave me a negative reputation thing. What's up with that Killer_Typo?

No, that means I didn't look at it because you didn't post the errors.

thats where it came from, from not even taking the time to look over it, even if it was just a breif code a simple, no thats not right, but post your code. at that point i wasnt looking for errors, i was looking for direction.

try this...

$result = mysql_query("SELECT * FROM users WHERE username = \"$_The_Name_Your_Checking\"");
if($myrow = mysql_fetch_assoc($result))
{
      echo("
               Name Taken...
      ");
} else {
      //Continue functions
}

Mysql doesn't match CaSe so if the user is trying to make the name "Dave" and there is alrdy a user in the db with the "dave" or "Dave" etc. mysql_fetch_assoc(); will be valid be $result actually extracted a row from the db. Else mysql_fetch_assoc(); will fail becase $result == NULL meaning that there are no daves in the table.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.