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

Link to comment
Share on other sites

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";
}
?>

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.