Jump to content

[SOLVED] login/registration form problem


JamesThePanda

Recommended Posts

Hi

I have been watching this video on you tube about making your own php login form. I can' seem to get it to work for me

 

 

here is the coe for the form im using at the moment

<html>
<BODY>
</BODY>
<form method="post" action= "logincheck.php" name="form1">
<label for="username">Username:</label>
<input type="text" name="myusername" id="myusername" />
<label for="password">Password:</label>
<input type="text" name="mypassword" id="mypassword" />
<input type="submit" name="submit" value="login"/>
</form>
</html>

 

and here is the code for the scipt

 

<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "members";
$tbl_name = "memtable";

mysql_connect( '$host', '$username', '$password') or die (mysql_error());
mysql_select_db($db_name) or die (mysql_error());

$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];

$sql = "SELECT * FROM $tbl_name WHERE username = '$myusername' and password = '$mypassword';
$result = mysql_query($sql);

$count = mysql_num_rows($result);

if ($count==1){
session_register("myusername");
session_register("muypassword");
header("location:login_success.php");
}
else {
echo "wrong username or password";
}
?>

 

I cant seem to understand what the problem is.

 

It is saying there is a problem on line20

 

Thanks

James

 

 

Link to comment
https://forums.phpfreaks.com/topic/181475-solved-loginregistration-form-problem/
Share on other sites

try this instead:

 

<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "members";
$tbl_name = "memtable";

mysql_connect($host,$username,$password) or die (mysql_error());
mysql_select_db($db_name) or die (mysql_error());

$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];

$sql = "SELECT * FROM $tbl_name WHERE username = '$myusername' and password = '$mypassword'";
$result = mysql_query($sql);

$count = mysql_num_rows($result);

if ($count==1){
session_register("myusername");
session_register("muypassword");
header("location:login_success.php");
}
else {
echo "wrong username or password";
}
?>

 

You were missing a closing " on your query. Also I removed the ' around the $host,$username,and $password in the mysql_connect as it is not needed.

yes you also need to start the session. however it is not needed at the top of the page in this case since it is the login. you can start the session when you know the information is correct. so try this:

 

<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "members";
$tbl_name = "memtable";

mysql_connect($host,$username,$password) or die (mysql_error());
mysql_select_db($db_name) or die (mysql_error());

$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];

$sql = "SELECT * FROM $tbl_name WHERE username = '$myusername' and password = '$mypassword'";
$result = mysql_query($sql);

$count = mysql_num_rows($result);

if ($count==1){
session_start();
session_register("myusername");
session_register("muypassword");
header("location:login_success.php");
}
else {
echo "wrong username or password";
}
?>

That doesnt make any sense looking at the code that I provided above. The only thing that I can think of is that you forgot the $ in front of the variable name. I would make sure you copied the code correctly and that it is registered on the server correctly.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.