php登录注册脚本错误

时间:2015-04-05 19:00:07

标签: php login

我是关于php的新手并试图创建一个注册和登录脚本,但我遇到了一些问题。当我提交注册表时,它显示错误消息:

  

注意:未定义的索引:在第8行的C:\ xampp \ htdocs \ hms3 \ actions.php中注册

    <?php
    require_once("config.php");
    if(isset($_REQUEST['login']))
    {
        $obj=new configs;
        $obj->login();
    }
    elseif($_REQUEST['register'])
    {
        $obj= new configs;
        $obj->register();
    }
?>

以上代码在actions.php

    <?php
session_start();
$_POST['msg']="works";
class configs
{
    function login()
    {
    $con = mysqli_connect("localhost","dbuser","12345","hms");
    // Check connection
    if (mysqli_connect_errno()) 
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

        $username=$_REQUEST['username'];
        $password=$_REQUEST['password'];
        $result = mysqli_query($con,"SELECT * FROM user WHERE username = '".$username."'");

        $row = mysqli_fetch_array($result);
                $_SESSION['username']=$row['username'];
                $_SESSION['password']=$row['password'];

        if($username==$row['username'] && $password==$row['password'])
        {
            header("location:index.php");
        }
        else
        {
            header("location:Login.php?msg=usernamepasswordincorrect");
        }
    }

    function register()
    {
        $username=$_REQUEST['username'];
        $password=$_REQUEST['password'];

        if($username!=""&&$password!="")
        {
            $con = mysqli_connect("localhost","dbuser","12345","hms");
            // Check connection
            if (mysqli_connect_errno()) 
            {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }

            $username=$_REQUEST['username'];
            $password=$_REQUEST['password'];

            $result = mysqli_query($con,"SELECT * FROM user WHERE username = '".$username."'");
            $row = mysqli_fetch_array($result);
            if($row['username']!=$username)
            {
            mysqli_query($con,"INSERT INTO user (username, password) VALUES ('$username', '$password')");
            header("location:register.php?msg=success");
            }
            else
            {
                header("location:register.php?msg=usernameexists");
            }
        }
        elseif($username=""&&$password="")
        {
            header("location:register.php?msg=both");
        }
    }
}
?>

以下注册表格HTML代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register Here</title>
</head>
<body>
<center>
<?php
if(isset($_REQUEST['msg']))
{
    if($_REQUEST['msg']=='success')
    {
        echo "Registration Successful. Now You Can Login To Your Account <a href='login.php'>Here</a>";
    }
    elseif($_REQUEST['msg']=='usernameexists')
    {
        echo "Username Already Exists";
    }
    elseif($_REQUEST['msg']=='both')
    {
        echo "Please Enter A Username And Password";
    }
}
?>
<h1>Register Here</h1>
<form action="actions.php" method="post">
<fieldset style="width: 650px;">
<legend>Patient Details</legend>
<table>
<tbody><tr>
<td>
<label for="firstname">First Name:</label>
</td>
<td><input name="firstname" type="text"></td>
<td><label for="lastname">Last Name:</label></td>
<td><input name="lastname" type="text"></td>
</tr>
<tr>
<td>
<label for="occupation">Occupation:</label>
</td>
<td><input name="occupation" type="text"></td>
<td><label for="religion">Religion:</label></td>
<td><input name="religion" type="text"></td>
</tr>
<tr>
<td><label for="birthday">Date of Birth</label></td>
<td><input name="birthday" type="text"></td>
<td><label for="sex">Sex</label></td>
<td><input name="sex" value="male" checked="" type="radio">Male
<input name="sex" value="female" type="radio">Female
</td>
</tr>
<tr>
<td><label for="telephone">Telephone:</label></td>
<td><input name="telephone" type="text"></td>
<td><label for="mobile">Mobile:</label></td>
<td><input name="mobile" type="text"></td>
</tr>
<tr>
<td><label for="presentaddress" />Present Address:</label></td>
<td><textarea name="presentaddress"></textarea></td>
<td><label for="permanentaddress" />Permanent Address:</label></td>
<td><textarea name="permanentaddress"></textarea></td>
</tr>
</tbody></table></fieldset>
<fieldset style="width: 650px;">
<table>
<legend>Medical Details</legend>
<tr>
<td><label for="diseasestype" />Diseases Type:</label></td>
<td><input type="text" name="diseasestype" /></td>
<td><label for="diseases" />Diseases Name:</label></td>
<td><input type="text" name="diseases" /></td>
</tr>

<tr>
<td><label for="referd" />Refd. By:</label></td>
<td><input type="text" name="refd" /></td>
<td><label for="specialist" />Specialist:</label></td>
<td><input type="text" name="specialist" /></td>
</tr>
<tr>
<td><label for="serial" />Serial Number:</label></td>
<td><input type="text" name="serial" /></td>
<td><label for="payment" />Payment:</label></td>
<td><input type="text" name="payment" /></td>
</tr>
<tr>
<td><label for="ward" />Ward No.:</label></td>
<td><input type="text" name="ward" /></td>
<td><label for="bed" />Bed No.:</label</td>
<td><input type="text" name="bed" /></td>
</tr>
<tr>
<td><label for="username" />Username:</label></td>
<td><input type="text" name="username" /></td>
<td><label for="password" />Password:</label</td>
<td><input type="password" name="password" /></td>
</tr>
</table>
</fieldset>
<input type="submit" value="Submit Details" />
</form>
</center>
</body>
</html>

现在我正在尝试在用户登录时显示个人资料信息。

<?php
session_start();
if(isset($_SESSION['username']))
{
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Only People Who Are Logged In Will See This Page</title>
</head>
<body>
<center>
<?php
$servername = "localhost";
$username = "dbuser";
$password = "12345";
$dbname = "hms";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT firstname, lastname FROM patient";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<table><tr><th>ID</th><th>Name</th></tr>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td></tr>";
     }
     echo "</table>";
} else {
     echo "0 results";
}

$conn->close();
?>
</center>
</body>
</html>';
}
else
{
    header("location:login.php?msg=you");   
}
?>

1 个答案:

答案 0 :(得分:0)

此错误

  

注意:未定义的索引:在C:\ xampp \ htdocs \ hms3 \ actions.php中注册   在第8行

是因为在您的表单中没有name=register的任何元素所以当您想要使用时$_REQUEST['register']说错误