单击表单按钮

时间:2016-02-04 12:33:38

标签: php mysql

当我在表单中选择我的按钮并指向登录处理页面的链接时,它只返回服务器错误500,我之前没有遇到过这种情况,并且谷歌没有运气。

这是我在登录页面上的HTML标记

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styles.css">
<title>Administrator Login</title>
</head>

<body>
<div class="container">
<div class="header"><a href="index.php"><img src="IMAGES/LOGO.png" alt="Insert Logo Here" name="Insert_logo" width="" height="90" id="Insert_logo" style="background-color: #; display:block;" /></a> 
<!-- end .header --></div>
<div class="content">
<form action="loginProcess.php" method="post" class="loginForm">
    <input type="text" id="username" name="username" placeholder="Enter Username" required>
    <input type="password" id="password" name="password" placeholder="Enter     Password" required>
        <button type="submit" id="loginBTN" >Login</button>
</form>
</div>

</body>
</html>

这是我的php进程的代码

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

 <?php
 error_reporting(E_ALL); ini_set('display_errors', 1);

 //stores the search data
$username = $_POST['username'];
$password = $_POST['password'];

//checks to see if it is empty or null
if(isset($username) && !empty($username) && (isset($password) &&    !empty($password))){
require('includes/dbconx.php');

 //escapes all special characters that could break database

 $pword = mysqli_real_escape_string($con, $password);
 $uname = mysqli_real_escape_string($con, $username);
 //searchq stores the cleaned up search data

 //create a variable to store a wildcard SQL statement

  $sql = mysqli_query($con, "SELECT * FROM login WHERE username = '%$uname%'  AND password = '%$pword%' ");
}//end statement

 //if no data is inserted it will putput this
 else{
    echo("Please enter Login details!");

    //this will kill the connection 

    die;
}
//end else
$result = $con->query($sql);
//if it finds no matching data it informs the user and kills the DB  connextion
if(mysqli_num_rows($result) == 0){
echo("<p>No record found or password doesn't match! </p>");
die;
}
else{
    header('Location: adminPage.php');

?>

</body>
</html>

这是我的连接,这适用于其他页面,因为它应该这样。

<?php
//connects to my music database
$con=mysqli_connect("localhost","root","root","music");
//if it fails to connect it outputs this with an error number
if(mysqli_connect_errno()) {
echo "failed to connet to MYSQL:".mysqli_connect_error();
}
?> 

1 个答案:

答案 0 :(得分:1)

有时间喝啤酒吗?

此查询不正确,仅在使用LIKE语法时才使用%字符,因此查询应为

$sql = mysqli_query($con, "SELECT * 
                           FROM login 
                           WHERE username = '$uname'  
                             AND password = '$pword' ");

如果您更加一致地格式化代码,它也有助于发现错误。

在我对上一个问题的回答中,请检查所有mysqli_次来电后的错误。在开发过程中,它将为您节省大量时间,因为当我们的开发人员做出很少的博客时

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>    
<body>    
<?php
    error_reporting(E_ALL); 
    ini_set('display_errors', 1);

    // this would be better at the top of the script
    require('includes/dbconx.php');    

    // why do 2 steps when one would do
    // also you have not checked these actually exist
    // until the IF that follows these 2 lines
    //$username = $_POST['username'];
    //$password = $_POST['password'];

    // empty() does an isset already so you only need the empty()
    if( !empty($username) && !empty($password)){  
        $pword = mysqli_real_escape_string($con, $_POST['password']);
        $uname = mysqli_real_escape_string($con, $_POST['username']);

        $sql = mysqli_query($con, "SELECT * 
                                   FROM login 
                                   WHERE username = '$uname'
                                    AND password = '$pword'");

       // always check status after mysqli_query and other calls
       // and at least output the error message
       if ( $sql === FALSE ) {
           echo mysqli_error($con);
           exit;
       }

    } else {
        echo("Please enter Login details!");
        die;
    }

    $result = $con->query($sql);
    if(mysqli_num_rows($result) == 0){
        echo("<p>No record found or password doesn't match! </p>");
        die;
    } else {
        header('Location: adminPage.php');
        // header Location: shoudl always be followed by an exit;
        // as header does not stop execution
        exit;

     } // add missing closing bracket
?>
</body>
</html>