Jump to content

[SOLVED] A few questions about this sign up script that won't work...


bobleny

Recommended Posts

Hi, I had a few questions about this sign up script that I was hoping you could answer!

 

I felt the easiest way to explain what is going on and ask my questions, was to use comments in the script it's self.

 

So, if you wouldn't mind reading through the script real quick and answering some of my questions, I would be very great full!

 

Also, please feel free to point out any mistakes or make suggestions that will make my script better....

 

Thanks!

<?php
session_start();
$database_hostname = "localhost";
$database_username = "root";
$database_password = "";

# Connect to my database.
mysql_connect($database_hostname, $database_username, $database_password);
# At this point I would like to know
# if it was able to connect to the database.
# How do I do that?

# Select database.
mysql_select_db("testy");
# Again, I would like to know
# if it was able to select the database.
# It should be the same as above, right?


# This next part I am trying to check to
# see if the username the person entered
# is taken.
# Is there a better way of doing this?
$query = mysql_query("SELECT 'name' FROM 'users' WHERE 'name' = '" . $_POST['sign_username'] . "'");
if (!$query)
# So, if there is no query, then the username
# must not be taken... So I think...
{
if($_POST['sign_password'] == $_POST['sign_varpassword'])
# If the 2 passwords entered match...
{
	$md5 = md5($_POST['sign_password']);
	$sign_password = SHA1($md5);
	# Yes, I know md5(SHA1($pass)) is redundant!

	$sign_username = $_POST['sign_username'];
	$sign_lvl = "Super Noob";
	$sign_date = date('l, F jS\, Y');

	# I can't get this part to work!
	# It sets the session and works the function
	# but it wont insert anything into the database.
	# What is wrong with it?
	mysql_query("INSERT INTO 'users' (`name`, `password`, `level`, `signup`, `id`) VALUES ('" . $sign_username . "', '" . $sign_password . "', '" . $sign_level . "', '" . $sign_date . "' , )");
	mysql_close();

	$_SESSION['now_sign'] = TRUE;
	sendem(signup, .1);
}
else
{
	$_SESSION['wrong_sign_password'] = TRUE;
	sendem(signup, .1);
}
}
else
{
$_SESSION['wrong_sign_username'] = TRUE;
sendem(signup, .1);
}
?>

 

Thanks!

Link to comment
Share on other sites

i would do this part deferently:

 

mysql_query("INSERT INTO 'users' (`name`, `password`, `level`, `signup`, `id`) VALUES ('" . $sign_username . "', '" . $sign_password . "', '" . $sign_level . "', '" . $sign_date . "' , )");
	mysql_close();

 

like this:

$result = mysql_query("INSERT INTO 'users'  SET `name` = '" . $sign_username . "', `password` =, '" . $sign_password . "' `level` = '" . $sign_level . "', `signup` = '" . $sign_date . "'");
	mysql_close();

 

Anything but this looks good to me...

Link to comment
Share on other sites

Sorry,I may be wrong here but try this out. Change this:

$query = mysql_query("SELECT 'name' FROM 'users' WHERE 'name' = '" . $_POST['sign_username'] . "'");

 

to this:

$username=$_POST['username']

$query = mysql_query("SELECT name FROM users WHERE name = '$username'"

 

And this:

"INSERT INTO 'users' (`name`, `password`, `level`, `signup`, `id`) VALUES ('" . $sign_username . "', '" . $sign_password . "', '" . $sign_level . "', '" . $sign_date . "' , )");

mysql_close();

 

To this:

$SQL="INSERT INTO `users` (`name`, `password`, `level`, `signup`, `id`) VALUES ("' . $sign_username . "', '" . $sign_password . "', '" . $sign_level . "', now() )";

$res=mysql_query($sql)or die(mysql_error());

 

 

 

 

 

Link to comment
Share on other sites

In the code you providing it is showing single quotes around your table name- 'users' -  it should be backtics like this `users`.

You only want single quotes for your values.  The other part I put in about mysql_error was just to show you if there was an error in the query. Then it will tell you what was wrong.

Link to comment
Share on other sites

you suggested using this:

$username=$_POST['username']

 

how do you no thats what the users text field is called, by there code t should be this:

$username=$_POST['sign_username']

 

here is the code with the answered questions:

 

<?php
session_start();
$database_hostname = "localhost";
$database_username = "root";
$database_password = "";

# Connect to my database.
if(!mysql_connect($database_hostname, $database_username, $database_password)){
echo "Failed to connect!";
exit;
}
# At this point I would like to know
# if it was able to connect to the database.
# How do I do that?

#A: Just use an if statement, it returns false if it fails.

# Select database.
if(!mysql_select_db("testy")){
echo "Failed to select database.";
exit;
}
# Again, I would like to know
# if it was able to select the database.
# It should be the same as above, right?

#A: Use an if statement again.


# This next part I am trying to check to
# see if the username the person entered
# is taken.
# Is there a better way of doing this?

#A: First i would check if they entered a username. Then basicly like you've done except get the number of rows or you can get the affected rows.
$username = $_POST['sign_username'];
if($username == ""){
echo "You need a username!";
exit;
}

$query = mysql_num_rows(mysql_query("SELECT `name` FROM `users` WHERE `name`='{$username}'"));
if ($query == 0)
# I changed it, so if there are no rows the username is available.
{
if($_POST['sign_password'] == $_POST['sign_varpassword'])
# If the 2 passwords entered match...
{
	$md5 = md5($_POST['sign_password']);
	$sign_password = SHA1($md5);
	# Yes, I know md5(SHA1($pass)) is redundant!

	$sign_username = $_POST['sign_username'];
	$sign_lvl = "Super Noob";
	$sign_date = date('l, F jS\, Y');

	# I can't get this part to work!
	# It sets the session and works the function
	# but it wont insert anything into the database.
	# What is wrong with it?

                # Is the ID field an auto-increment, because it should be, if it is you dont need to write it in.
	if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_lvl}', now())")){
echo "Query failed!";
exit;
}

	mysql_close();

	$_SESSION['now_sign'] = TRUE;
	sendem(signup, .1);
}
else
{
	$_SESSION['wrong_sign_password'] = TRUE;
	sendem(signup, .1);
}
}
else
{
$_SESSION['wrong_sign_username'] = TRUE;
sendem(signup, .1);
}
?>

 

there may be some i missed out on, sorry but see if that works.

Link to comment
Share on other sites

oooo! Thanks ProjectFear, this is great! I haven't tried it yet though....

 

I had a question about it though, what does the "now()" do in the following line?:

"if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_lvl}', now())"))"

 

Thanks again!

Link to comment
Share on other sites

lol.

1. now() just gets the current date and time...

2. no they dont, i dont think, i just made it like that because its easier to read.

3. they just seperate variables from normal text, you dont have to do it like that but its good with arrays because if you dont sometimes you get errors.

 

Link to comment
Share on other sites

No, it still isn't working... I am having the same problem but thanks to everyones help, I am getting near the end of this dang problem. :)

 

Now, this code here is still giving me a problem... The problem with this script,

 

<?php
if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_level}','{$sign_date}'"))
{
$_SESSION['error_message'] = mysql_error();
mysql_close();
sendem(error, .1);
}
else
{
mysql_close();
$_SESSION['now_sign'] = TRUE;
sendem(signup, .1);
}
?>

 

When I arrive at the error page, I receive this error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

What does this mean? If you need to see the entire script, I can show you, but you wont like it!

 

And again, I am very great full for any and all help!

Link to comment
Share on other sites

Here is the same thing I gave you above but after I modifyed it, with your help of course!

 

<?php
if(!mysql_connect($database_hostname, $database_username, $database_password))
{
sendem(error, .1);
}

if(!mysql_select_db("testy"))
{
sendem(error, .1);
}

if($_POST['sign_username'] == "")
{
echo "You need a username!";
}

$query = mysql_query("SELECT `name` FROM `users` WHERE `name`='{$_POST['sign_username']}'");
$rows = mysql_num_rows($query);
if ($rows == 0)
{
if($_POST['sign_password'] == $_POST['sign_varpassword'])
{
	$md5 = md5($_POST['sign_password']);
	$sign_password = SHA1($md5);
	$sign_username = $_POST['sign_username'];
	$sign_lvl = "Super Noob";
	$sign_date = date('l, F jS\, Y');

	if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_level}','{$sign_date}'"))
	{
		$_SESSION['error_message'] = mysql_error();
		mysql_close();
		sendem(error, .1);
	}
	else
	{
		mysql_close();
		$_SESSION['now_sign'] = TRUE;
		sendem(signup, .1);
	}
}
else
{
	$_SESSION['wrong_sign_password'] = TRUE;
	sendem(signup, .1);
}
}
else
{
$_SESSION['wrong_sign_username'] = TRUE;
sendem(signup, .1);
}
?>

Link to comment
Share on other sites

Ok, Please ignore the last two posts! I have continued to modify the script and this seems even more accurate but I still don't understand....

 

This is there error message:

Access denied for user 'www-data'@'localhost' (using password: NO)

 

This is my little thingy to tell me about what line the error has occurred at:

Line: 153

 

This is the surrounding area of line: 153:

<?php
if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_level}','{$sign_date}'"))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 153";
mysql_close();
sendem(error, .1);
}
else
{
mysql_close();
$_SESSION['now_sign'] = TRUE;
sendem(signup, .1);
}
?>

 

This is the latest version of the script I am working on:

<?php
if(!mysql_connect($database_hostname, $database_username, $database_password))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 111";
mysql_close();
sendem(error, .1);
}

if(!mysql_select_db("testy"))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 119";
mysql_close();
sendem(error, .1);
}

if(!$query = mysql_query("SELECT `name` FROM `users` WHERE `name`='{$_POST['sign_username']}'"))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 127";
mysql_close();
sendem(error, .1);
}

if(!$rows = mysql_num_rows($query))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 135";
mysql_close();
sendem(error, .1);
}

if ($rows == 0)
{
if($_POST['sign_password'] == $_POST['sign_varpassword'])
{
	$md5 = md5($_POST['sign_password']);
	$sign_password = SHA1($md5);
	$sign_username = $_POST['sign_username'];
	$sign_lvl = "Super Noob";
	$sign_date = date('l, F jS\, Y');

	if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_level}','{$sign_date}'"))
	{
		$_SESSION['error_message'] = mysql_error();
		$_SESSION['error_location'] = "Line: 153";
		mysql_close();
		sendem(error, .1);
	}
	else
	{
		mysql_close();
		$_SESSION['now_sign'] = TRUE;
		sendem(signup, .1);
	}
}
else
{
	$_SESSION['wrong_sign_password'] = TRUE;
	sendem(signup, .1);
}
}
else
{
$_SESSION['wrong_sign_username'] = TRUE;
sendem(signup, .1);
}
?>

Link to comment
Share on other sites

that wouldn't return much actually.

try it like this, assuming $rows is equal to 0.

 

if($rows == mysql_num_rows($query))
{
   $_SESSION['error_message'] = mysql_error();
   $_SESSION['error_location'] = "Line: 135";
   mysql_close();
   sendem(error, .1);
}

 

or you could just check to see if mysql_num_rows($query) == 0.

Link to comment
Share on other sites

I retract my last statement, I don't believe that is the problem, maybe a problem, but not the problem...

 

<?php
if(!mysql_connect($database_hostname, $database_username, $database_password))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 111";
mysql_close();
sendem(error, .1);
}
?>

If it was a connection error, "Line:" would return 111, but it didn't, it returned 153.

 

Because it returned 153, the error pertains to this part here:

<?php
if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_level}','{$sign_date}'"))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 153";
mysql_close();
sendem(error, .1);
}
?>

This tells me then, I am loosing connection some where between the initial MySQL connect and the query...

 

That is when I thought maybe, if "!$rows" is the same as saying "$row = 0". This made me think that maybe this was being executed:

<?php
if(!$rows == mysql_num_rows($query))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 135";
mysql_close();
sendem(error, .1);
}
?>

 

However, if that was the case, "Line: 135" would have appeared. Because it did not, then everything is OK, and I have connection even at this point.

 

Because I have connection at this point, the only thing left is this, which is where the script tells me the problem is:

<?php
if ($rows == 0)
{
if($_POST['sign_password'] == $_POST['sign_varpassword'])
{
	$md5 = md5($_POST['sign_password']);
	$sign_password = SHA1($md5);
	$sign_username = $_POST['sign_username'];
	$sign_lvl = "Super Noob";
	$sign_date = date('l, F jS\, Y');

	if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_level}','{$sign_date}'"))
	{
		$_SESSION['error_message'] = mysql_error();
		$_SESSION['error_location'] = "Line: 153";
		mysql_close();
		sendem(error, .1);
	}
	else
	{
		mysql_close();
		$_SESSION['now_sign'] = TRUE;
		sendem(signup, .1);
	}
}
else
{
	$_SESSION['wrong_sign_password'] = TRUE;
	sendem(signup, .1);
}
}
else
{
$_SESSION['wrong_sign_username'] = TRUE;
sendem(signup, .1);
}
?>

 

Now, because I have no problem until that point, my question is, what is wrong?

Link to comment
Share on other sites

This is the latest version of the script I am working on:

<?php
if(!mysql_connect($database_hostname, $database_username, $database_password))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 111";
mysql_close();
sendem(error, .1);
}

if(!mysql_select_db("testy"))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 119";
mysql_close();
sendem(error, .1);
}

if(!$query == mysql_query("SELECT `name` FROM `users` WHERE `name`='{$_POST['sign_username']}'"))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 127";
mysql_close();
sendem(error, .1);
}

if(!$rows == mysql_num_rows($query))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 135";
mysql_close();
sendem(error, .1);
}

if ($rows == 0)
{
if($_POST['sign_password'] == $_POST['sign_varpassword'])
{
	$md5 = md5($_POST['sign_password']);
	$sign_password = SHA1($md5);
	$sign_username = $_POST['sign_username'];
	$sign_lvl = "Super Noob";
	$sign_date = date('l, F jS\, Y');

	if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_level}','{$sign_date}'"))
	{
		$_SESSION['error_message'] = mysql_error();
		$_SESSION['error_location'] = "Line: 153";
		mysql_close();
		sendem(error, .1);
	}
	else
	{
		mysql_close();
		$_SESSION['now_sign'] = TRUE;
		sendem(signup, .1);
	}
}
else
{
	$_SESSION['wrong_sign_password'] = TRUE;
	sendem(signup, .1);
}
}
else
{
$_SESSION['wrong_sign_username'] = TRUE;
sendem(signup, .1);
}
?>

Link to comment
Share on other sites

Ok, Please ignore the last two posts! I have continued to modify the script and this seems even more accurate but I still don't understand....

 

This is there error message:

Access denied for user 'www-data'@'localhost' (using password: NO)

 

This is my little thingy to tell me about what line the error has occurred at:

Line: 153

 

This is the surrounding area of line: 153:

<?php
if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_level}','{$sign_date}'"))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 153";
mysql_close();
sendem(error, .1);
}
else
{
mysql_close();
$_SESSION['now_sign'] = TRUE;
sendem(signup, .1);
}
?>

 

This is the latest version of the script I am working on:

<?php
if(!mysql_connect($database_hostname, $database_username, $database_password))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 111";
mysql_close();
sendem(error, .1);
}

if(!mysql_select_db("testy"))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 119";
mysql_close();
sendem(error, .1);
}

if(!$query = mysql_query("SELECT `name` FROM `users` WHERE `name`='{$_POST['sign_username']}'"))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 127";
mysql_close();
sendem(error, .1);
}

if(!$rows = mysql_num_rows($query))
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 135";
mysql_close();
sendem(error, .1);
}

if ($rows == 0)
{
if($_POST['sign_password'] == $_POST['sign_varpassword'])
{
	$md5 = md5($_POST['sign_password']);
	$sign_password = SHA1($md5);
	$sign_username = $_POST['sign_username'];
	$sign_lvl = "Super Noob";
	$sign_date = date('l, F jS\, Y');

	if(!mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_level}','{$sign_date}'"))
	{
		$_SESSION['error_message'] = mysql_error();
		$_SESSION['error_location'] = "Line: 153";
		mysql_close();
		sendem(error, .1);
	}
	else
	{
		mysql_close();
		$_SESSION['now_sign'] = TRUE;
		sendem(signup, .1);
	}
}
else
{
	$_SESSION['wrong_sign_password'] = TRUE;
	sendem(signup, .1);
}
}
else
{
$_SESSION['wrong_sign_username'] = TRUE;
sendem(signup, .1);
}
?>

Link to comment
Share on other sites

Superuser2, you are correct. After I fixed my little error setup, I found that this if statement is proving true:

<?php
$rows = mysql_num_rows($query);
if(!$rows)
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Line: 136";
mysql_close();
sendem(error, .1);
die();
}
?>

The server is not returning a mysql error! So, I haven't the slightest idea what is wrong with it. What other types of error functions can I use?

 

 

Again, here is the latest version after the fix:

<?php
if($_SESSION['now_sign'] == FALSE)
{
if($_SESSION['wrong_sign_password'] == TRUE)
{
	$_SESSION['wrong_sign_password'] = FALSE;
	echo "Your passwords did not match! Remember, your password is case sensitive! \r\n <br /> \r\n";
	echo "Please try again... \r\n <br /> \r\n";
}
elseif($_SESSION['wrong_sign_username'] == TRUE)
{
	$_SESSION['wrong_sign_username'] = FALSE;
	echo "I'm sorry, that usermane is already taken. \r\n <br /> \r\n";
	echo "Please try again... \r\n <br /> \r\n";
}
else
{
	echo "In order to start useing the site forum and all of the site goodies you must be signed up. To do so, simply fill out the forum below. Your password is case sensitive! \r\n <br /> \r\n";
}
echo "<form action='index.php?page=signup' method='post'> \r\n";
echo "Username: <input type='text' name='sign_username' size='21' maxlength='10'> \r\n <br /> \r\n";
echo "Password: <input type='password' name='sign_password' size='21' maxlength='20'> \r\n <br /> \r\n";
echo "Verify Password <input type='password' name='sign_varpassword' size='21' maxlength='20'> \r\n <br /> \r\n";
echo "<input type='submit' value='Sign Up'> \r\n";
echo "</form>";
}

if($_SESSION['now_sign'] == TRUE)
{
$_SESSION['now_sign'] = FALSE;
echo "Congratulations, you are now signed up! You are now being redirected! \r\n <br /> <br /> \r\n <a href='index.php?page=home'>If you are not automaticly redirected in 10 seconds, please click here.</a> \r\n";
sendem(home, 5);
}

if(isset($_POST['sign_username']) == TRUE)
{
$connect = mysql_connect($database_hostname, $database_username, $database_password);
if(!$connect)
{
	$_SESSION['error_message'] = mysql_error();
	$_SESSION['error_location'] = "Line: 106";
	mysql_close();
	sendem(error, .1);
	die();
}

$selectdb = mysql_select_db("testy");
if(!$selectdb)
{
	$_SESSION['error_message'] = mysql_error();
	$_SESSION['error_location'] = "Line: 116";
	mysql_close();
	sendem(error, .1);
die();
}

$query = mysql_query("SELECT `name` FROM `users` WHERE `name`='{$_POST['sign_username']}'");
if(!$query)
{
	$_SESSION['error_message'] = mysql_error();
	$_SESSION['error_location'] = "Line: 126";
	mysql_close();
	sendem(error, .1);
	die();
}

$rows = mysql_num_rows($query);
if(!$rows)
{
	$_SESSION['error_message'] = mysql_error();
	$_SESSION['error_location'] = "Line: 136";
	mysql_close();
	sendem(error, .1);
	die();
}

if ($rows == 0)
{
	if($_POST['sign_password'] == $_POST['sign_varpassword'])
	{
		$md5 = md5($_POST['sign_password']);
		$sign_password = SHA1($md5);
		$sign_username = $_POST['sign_username'];
		$sign_lvl = "Super Noob";
		$sign_date = date('l, F jS\, Y');

		$query = mysql_query("INSERT INTO `users` (`name`, `password`, `level`, `signup`) VALUES ('{$sign_username}','{$sign_password}','{$sign_level}','{$sign_date}'");
		if(!$query)
		{
			$_SESSION['error_message'] = mysql_error();
			$_SESSION['error_location'] = "Line: 156";
			mysql_close();
			sendem(error, .1);
			die();
		}
		else
		{
			mysql_close();
			$_SESSION['now_sign'] = TRUE;
			sendem(signup, .1);
		}
	}
	else
	{
		$_SESSION['wrong_sign_password'] = TRUE;
		sendem(signup, .1);
	}
}
else
{
	$_SESSION['wrong_sign_username'] = TRUE;
	sendem(signup, .1);
}
}
?>

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.