I am new to php, anyway im working on a text based game with a friend. So I started out with the basics, we planned out the game and decided how it would work. Then I setup most of the MySQL DB. After that I started working on a script that would add the user info to the db. So far it takes the users name, password and email, checks them for length and characters then is suppose to connect to mysql and check if the username exists already.
Well something goes wrong with the sql part, i know it can connect to the db I have tested that. I think this is a simple php error.
I have highlighted the parts that do the sql stuff that could be problematic, if someone can help me with this it would be much appreciated.
<?php
// db.php by smartkid
function db_connect()
{
// DB Info
$dbhost = '72.29.87.3';
$dbuser = '--------';
$dbpass = '--------';
$dbname = 'megaman_CyberStorm';
// Connects to db or dies
$db = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Error connecting to mysql');
//Selects Our DB
mysql_select_db($dbname);
// gives back $db info to use to disconnect
return($db);
}
function query($query)
{
$result = mysql_query($query);
}
function username_check($user, $result)
{
$query = "SELECT username FROM user_info WHERE username = '$user'";
mysql_query($query);
$result = 2; //mysql_numrows($query);
return($user);
return($result);
}
function db_disconnect()
{
mysql_close();
}
?>
This file should be fine its just a form VVV
<?php
// register.php by Smartkid
?>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="check.php" method="POST">
<center>
<table border=0 cellspacing=1 cellpadding=0 width=300>
<tr>
<td colspan=2>Welcome to CyberStorm, please fill out the following information and then hit submit to proceed to step 2.<br></td>
</tr>
<tr>
<td align=right>Username:</td>
<td align=left><input type="text" name="user" maxlength="30" value=""></td>
</tr>
<tr>
<td align=right>Password:</td>
<td align=left><input type="password" name="pass" maxlength="30" value=""></td>
</tr>
<tr>
<td align=right>Email:</td>
<td align=left><input type="text" name="email" maxlength="50" value=""></td>
</tr>
<tr>
<td colspan=2 align=center><input type="submit" value="Join!"></td>
</tr>
</table>
</body>
</html>
<?php
// check.php by Smartkid
include("include/db.php");
$POST['user'] = $user;
$POST['pass'] = $pass;
$_POST['email'] = $email;
// Check username lenth
if(strlen($user) < 5)
{
echo("Username below 5 characters"); die;
}
//Make sure username is not to long
else if(strlen($user) > 30)
{
echo("Username above 30 characters"); die;
}
// Check if username is not alphanumeric
else if(!eregi("([0-9a-z])+$", $user))
{
echo("Username not alphanumeric"); die;
}
// Password Checking
else if(strlen($pass) < 5)
{
echo("Password below 5 characters"); die;
}
//Password checking
else if(strlen($pass) > 30)
{
echo("Password is over 30 characters"); die;
}
//Check if password is not alphanumeric
else if(!eregi("([0-9a-z])+$", $pass))
{
echo("Password not alphanumeric"); die;
}
// Email Checking
else if(strlen($email) < 5)
{
echo("Email is below 5 characters"); die;
}
//Email checking
else if(strlen($email) > 30)
{
echo("Email is over 30 characters"); die;
}
else
$regex = "[+a-z0-9-]+(.[+a-z0-9-]+)"."@[a-z0-9-]+(.[a-z0-9-]{1,})".".([a-z]{2,}){1}$";
if(!eregi($regex,$email))
{
echo("Email has bad charecters"); die;
}
else
db_connect();
$result = 1;
username_check($user, $result);
db_disconnect();
if ($result > 0)
{
echo("User already exists!".$result);
}
else
echo($result."<center>
<table border=0 cellspacing=1 cellpadding=0 width=300>
<tr><td colspan=2>Please check if this information is correct then hit submit. You cannot change your username or email after this step<br></td></tr>
<tr><td align=right>Username:</td><td align=left><input type=text readonly=true name=user maxlength=30 value=".$user."></td></tr>
<tr><td align=right>Password:</td><td align=left><input type=text readonly=true name=pass maxlength=30 value=".$pass."></td></tr>
<tr><td align=right>Email:</td><td align=left><input type=text readonly=ture name=email maxlength=50 value=".$email."></td></tr>
<tr><td colspan=2 align=center><br><input type=submit value=Submit></td></tr>
</table>");
?>
Thanks so much,
Sk~