Jump to content


Photo

Name and password verification


  • Please log in to reply
3 replies to this topic

#1 Thesus

Thesus
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 October 2006 - 09:21 PM

Well I'm trying to create a forum and have been doing so for a while, I just can never verify the user's password correctly. If they haven't been inserted into the database, it automatically assumes the names and password do not match. I thought I finally figured it out with this simplified-down code, but currently to no avail I just get a blank page. Help's very, very, very much appreciated. :P (Just to note aswell, this isn't a register/login forum. It's where you use a name and password, and that's it- no login)

<?php
$host="db1.awardspace.com";
$username="thesus_dantee";
$password="*********";
$db_name="thesus_dantee";
$tbl_name="regulars";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$name1 = $_POST['name'];
$password1 = $_POST['password'];

$name = addslashes('$name1');
$password = addslashes('$password1');

$check = "SELECT name FROM $tbl_name WHERE name='$name'";
$query = mysql_query($check);

if($query == 0){
$sql = "INSERT INTO $tbl_name(name, password)VALUES('$name', '$password')";
$result = mysql_query($sql);
echo "Successfully Posted!";
}
else
{
$check2 = "SELECT * FROM $tbl_name WHERE name='$name' AND password='$password'";
$query2 = mysql_query($check2);

$count = mysql_num_rows($query2);

if($count == 1){
$sql2="INSERT INTO $tbl_name(name, password)VALUES('$name', '$password')";
$result2=mysql_query($sql2);
echo "Sucessfully Posted!";
}
}
?>


#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 17 October 2006 - 03:09 AM

The logic in your script seems odd.  At the end, with $query2, you say "If the user and password exist in the database, then insert them again".  That doesn't make sense.  If they are already in the database, then any attempt to insert them will fail.  It should probably be

if ($count == 1) {
  echo "Username and password match!";
}

Also, it's a good idea to check the result of your second mysql query, just in case.

if ($query2 === false) {
  die("$query2 failed: " . mysql_error());
}

In this case, $query2 == 0 will give you the same result.

#3 Masna

Masna
  • Staff Alumni
  • Advanced Member
  • 288 posts
  • LocationNew York

Posted 17 October 2006 - 04:45 AM

Your queries are formatted incorrectly.

No {s allowed...
I like LoL.

#4 Thesus

Thesus
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 17 October 2006 - 05:07 AM

When you check the database for the name and password, and if that person has never postd before and has no information stored, it automatically assumes the name and password are incorrect and doesn't alow you to post. So the first query checks if the name is, infact there, and if it isn't it allows you to post and stores your password for the 2nd query for future postage. :D

And thanks, I'll try, and hopefully it'll work. :P




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users