Page 1 of 1

Need help with my PHP code.

Posted: Fri May 07, 2010 6:41 pm
by Qunox
I was thinking of making a bether registration page with the refresh technique.
But i'm getting some errors, i'v been trying like everything:

register.php:

Code: Select all

<?php
	include_once 'connect.php';
?>

<?php
		if(isset($_POST['username']))
		{
			$username = $_POST['username'];
			$password = $_POST['password'];
			$email	  = $_POST['email'];
			// ANTI SQL INJECTION \\
			$username = strip_tags($username);
			$password = strip_tags($password);
			$email = strip_tags($email);
			
			$username = stripslashes($username);
			$password = stripslashes($password);
			$email = stripslashes($email);
			
			$fetcharry=mysql_fetch_array(mysql_query("SELECT * from players where name='$username'"));
			if( (!$username) || (!$password) || (!$email) )
			{
			if(!$username){echo "Missing Username<br>";}
			if(!$password){echo "Missing Password<br>";}
			if(!$email){echo "Missing Email";}
			}
			else
			{

				mysql_query("INSERT into players(name, password, email) VALUES ('$player','$password','$email')");
			}
		}
?>
<form method="post" action="">
<b>Användarnamn:<input type="text" size="15" maxlength="21" name="username"></b>
<b>Lösenord:<input type="password" size="15" maxlenght="21" name="password"></b>
<b>Email:<input type="text" size="25" maxlength="55" name="email"></b></center><br><br>
<form 
<center><input type="submit" value="Registera"></center>

First of i KNOW i miss "or die()" statements and i'm missing multiple email and username checks but i just want to get it to work before i get starting on that. It's probebly a stupid mistake or miss or something but it gives me an error around line 30( which is mysql_query function)

Re: Need help with my PHP code.

Posted: Fri May 07, 2010 10:33 pm
by Jackolantern
Can you post the errors you are getting, and the line numbers?

Re: Need help with my PHP code.

Posted: Fri May 07, 2010 11:30 pm
by Qunox
Undefined variable: player in C:\wamp\www\shard_engine\index.php on line 30

Thats the error, but a weird thing, it all enters my database in my players tables, exept the name.... :S

Re: Need help with my PHP code.

Posted: Fri May 07, 2010 11:41 pm
by Jackolantern
You define $username as your player name at the top, but you are trying to add $player to your database, which does not appear to be a set variable.

Re: Need help with my PHP code.

Posted: Fri May 07, 2010 11:41 pm
by Qunox
Haha such a stupid error of me :P
But when we are on it, how should you properly check if their is a username already in the database?

Re: Need help with my PHP code.

Posted: Fri May 07, 2010 11:47 pm
by Jackolantern
I actually made the exact same error in my code, and studied it for over an hour before I found it, so don't feel bad XD

As far as making sure there are no other players with the name, run a SELECT SQL query to find any players with that name, then make sure there are no results, like:

Code: Select all

$result = mysql_fetch_array(mysqli_query("SELECT name FROM players WHERE name='$username'"));
if (!$result) {
     //No player with that name! OK to use!
}