-
[PHP + MySQL] Errors
xD Started PHP + MySQL today, and I decided to make a little text-based game.
I worked out registering, logging in, members area, logging out, and a members list.
Only problem is, logging in and registering doesn't completely work.
This is my register.php (it gets it information from a seperate html file)
Code:
<<?php
include("config.php");
// connect to the mysql server
$link = mysql_connect($servername, $dbusername, $dbpassword)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($dbname)
or die ("Could not select database because ".mysql_error());
// check if the username is taken
$check = "select id from $table where username = '".$_POST['username']."';";
$qry = mysql_query($check)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=register.html>Try again</a>";
exit;
} else {
// insert the data
$insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."', '".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());
// print a success message
echo "Your user account has been created!<br>";
echo "Now you can <a href=login.html>log in</a>";
}
?>
And here is my login.php
Code:
<?php
ob_start();
include("config.php");
// connect to the mysql server
$link = mysql_connect($servername, $dbusername, $dbpassword)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($dbname)
or die ("Could not select database because ".mysql_error());
$match = "select id from $table where username = '".$_POST['username']."'
and password = '".$_POST['password']."';";
$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows <= 0) {
echo "Sorry, there is no username $username with the specified password.<br>";
echo "<a href=login.html>Try again</a>";
exit;
} else {
setcookie("loggedin", "TRUE", time()+(3600 * 24));
setcookie("gonline_username", "$username");
setcookie("gonline_password", "$password");
setcookie("gonline_wepname", "$wepname");
setcookie("gonline_name", "$name");
setcookie("gonline_level", "$level");
echo "You are now logged in!<br>";
echo "Continue to the <a href=members.php>members</a> section.";
}
ob_end_flush();
?>
Logging in works, but doesn't actually show the values of username, pass, wepname, etc
When I try to register, this is what I get:
Could not insert data because Column count doesn't match value count at row 1
Live example:
http://gantzonline.com.net.sc/register.html
and for login
http://gantzonline.com.net.sc/login.html
-
re: [PHP + MySQL] Errors
Register
Although I don't know your column names you can replace ID, Username, and Password with your own.
PHP Code:
require_once 'config.php' ;
// connect to the mysql server
$link = mysql_connect($servername, $dbusername, $dbpassword)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($dbname)
or die ("Could not select database because ".mysql_error());
// check if the username is taken
$check = "select id from $table where username = '".$_POST['username']."'";
$qry = mysql_query($check)
or die ("Could not match data because ".mysql_error());
if (mysql_num_rows($qry) > 0) {
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=register.html>Try again</a>";
return false;
} else {
// insert the data
mysql_query("INSERT INTO $table (ID, Username, Password) values ('NULL', '".$_POST['username']."', '".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());
// print a success message
echo "Your user account has been created!<br>";
echo "Now you can <a href=login.html>log in</a>";
}
Login
Your problem here is that you arent fetching the information to be used in the cookies so its just putting blank information for $username, $password, Etc.
But as I said I don't know the column names so I can't help you there
PHP Code:
ob_start();
include("config.php");
// connect to the mysql server
$link = mysql_connect($servername, $dbusername, $dbpassword)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($dbname)
or die ("Could not select database because ".mysql_error());
$match = "select id from $table where username = '".$_POST['username']."'
and password = '".$_POST['password']."'";
$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
if (mysql_num_rows($qry) < 0) {
echo "Sorry, there is no username $username with the specified password.<br>";
echo "<a href=login.html>Try again</a>";
return false;
} else {
setcookie("loggedin", "TRUE", time()+(3600 * 24));
setcookie("gonline_username", "$username");
setcookie("gonline_password", "$password");
setcookie("gonline_wepname", "$wepname");
setcookie("gonline_name", "$name");
setcookie("gonline_level", "$level");
echo "You are now logged in!<br>";
echo "Continue to the <a href=members.php>members</a> section.";
}
ob_end_flush();
-
re: [PHP + MySQL] Errors
Ok, the registering now works, thanks so much. I can't get the logging in to completely work though...
I also forgot my members.php
Here is members.php
PHP Code:
<html>
<head>
<title>Members' Section</title>
</head>
<body>
<?php
if (!isset($_COOKIE['loggedin'])) die("You are not logged in!<br><a href=login.html>log in</a>");
$username = $HTTP_COOKIE_VARS["username"];
$password = $HTTP_COOKIE_VARS["password"];
$level = $HTTP_COOKIE_VARS["level"];
$name = $HTTP_COOKIE_VARS["name"];
$wepname = $HTTP_COOKIE_VARS["wepname"];
echo "Hello $name.";
echo "You are logged in as $username.<p> and you are level $level.<p>";
echo "Your password is $password. and the name of your weapon is $wepname.<p>";
?>
<a href="logout.php">Log out</a>
</body>
</html>
-
re: [PHP + MySQL] Errors
I'll help you out more later on. Msn = sk84life199090@hotmail.com add me and I'll help you finish it
PHP Code:
<?php
ob_start();
include("config.php");
// connect to the mysql server
$link = mysql_connect($servername, $dbusername, $dbpassword)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($dbname)
or die ("Could not select database because ".mysql_error());
$match = "select * from $table where username = '".$_POST['username']."'
and password = '".$_POST['password']."'";
$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
if (mysql_num_rows($qry) < 0) {
echo "Sorry, there is no username $username with the specified password.<br>";
echo "<a href=login.html>Try again</a>";
return false;
} else {
$Query = mysql_fetch_assoc($qry);
$_SESSION['Logged_in'] = 1;
$_SESSION['Username'] = $Query['username'];
$_SESSION['Password'] = $Query['password'];
$_SESSION['WepName'] = $Query['wepname'];
$_SESSION['name'] = $Query['name'];
$_SESSION['Level'] = $Query['level'];
echo "You are now logged in!<br>";
echo "Continue to the <a href=members.php>members</a> section.";
}
ob_end_flush();
?>
-
re: [PHP + MySQL] Errors
Don't forget to clean your POST data or someone will inject you like a crack addict.
-
re: [PHP + MySQL] Errors
xD I don't plan on making something big out of this, mostly just learning. But I will once I figure out how, I know the security risks, seen it done before.
-
re: [PHP + MySQL] Errors
PHP Code:
<?php
ob_start();
session_start();
?>
<html>
<head>
<title>Members' Section</title>
</head>
<body>
<?php
if (!isset($_SESSION['Logged_in']))
{
echo "You are not logged in!<br><a href=login.html>log in</a>";
return false;
} else {
$username = $_SESSION["Username"];
$password = $_SESSION["Password"];
$level = $_SESSION["Level"];
$name = $_SESSION["name"];
$wepname = $_SESSION["WepName"];
echo "Hello $name.";
echo "You are logged in as ".$username.".<p> and you are level ".$level.".<p>";
echo "Your password is ."$password.". and the name of your weapon is ".$wepname.".<p>";
}
?>
<a href="logout.php">Log out</a>
</body>
</html>