Home > Backend Development > PHP Tutorial > php写了个登陆,服务器报错500.

php写了个登陆,服务器报错500.

WBOY
Release: 2016-06-06 20:19:10
Original
1671 people have browsed it

代码如下:
表结构

<code>id         int 
phone      char
password   char</code>
Copy after login
Copy after login

Conn.php

<code><?php //设置数据库变量
    $db_host   = 'localhost';  //数据库主机名称,一般都为localhost
    $db_user   = 'root';        //数据库用户帐号,根据个人情况而定
    $db_passw = '123456';   //数据库用户密码,根据个人情况而定
    $db_name  = 'db';         //数据库具体名称
    
    //连接数据库
    $conn = mysql_connect($db_host,$db_user,$db_passw) or die ('数据库连接失败!</br>错误原因:'.mysql_error());
    //设置字符集,如utf8和gbk等
    mysql_query("set names 'utf8'");
    //选定数据库
    mysql_select_db($db_name,$conn) or die('数据库选定失败!错误原因:'.mysql_error());
    //执行SQL语句(查询)
    //$result = mysql_query($sql) or die('数据库查询失败!错误原因:'.mysql_error());
?></code>
Copy after login
Copy after login

login.php

<code><?php header('Content-Type:application/json;charset=utf-8');
        //判断请求类型
        if (isset($_POST['phone']) && isset($_POST['password'])) {
            // 获取POST请求参数
            $accessType = '[POST]';
            $userPhone = $_POST['phone'];
            $userPassword = $_POST['password'];
        } else {
            echo json_encode(array('result'=>'非法请求'));
            return false;
        }
        
        //创建SQL语句
        $sql="SELECT * FROM lms_users WHERE phone ='".$userName."' AND `password` = '".$userPassword."';";
    
        //调用conn.php文件进行数据库操作
        require('Conn.php');
        global $conn;
        
        $result=mysql_query($sql,$conn) or die('数据库查询失败!错误原因:'.mysql_error());
        
        $flag=0;
        while(!!$item=mysql_fetch_assoc($result)){
            $flag++;
        };
        //实际只能返回一条结果.
        if ($flag >= 1) {
            $result = array(
                            echo json_encode($row);
                           );
            echo json_encode($result);
            exit();
        }else {
            $result = array(
                            'id' => 0
                            );
            echo json_encode($result);
        }
        return true;
?></code>
Copy after login
Copy after login

login.html

<code>



    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>PHP 测试</title>


    
<p></p>
<h1>登陆</h1>
<hr>
<form action="login.php" method="post">
    <p>姓名: <input type="text" name="phone"></p>
    <p>密码: <input type="password" name="password"></p>
    <p><input type="submit"></p>
</form>


</code>
Copy after login
Copy after login

回复内容:

代码如下:
表结构

<code>id         int 
phone      char
password   char</code>
Copy after login
Copy after login

Conn.php

<code><?php //设置数据库变量
    $db_host   = 'localhost';  //数据库主机名称,一般都为localhost
    $db_user   = 'root';        //数据库用户帐号,根据个人情况而定
    $db_passw = '123456';   //数据库用户密码,根据个人情况而定
    $db_name  = 'db';         //数据库具体名称
    
    //连接数据库
    $conn = mysql_connect($db_host,$db_user,$db_passw) or die ('数据库连接失败!</br>错误原因:'.mysql_error());
    //设置字符集,如utf8和gbk等
    mysql_query("set names 'utf8'");
    //选定数据库
    mysql_select_db($db_name,$conn) or die('数据库选定失败!错误原因:'.mysql_error());
    //执行SQL语句(查询)
    //$result = mysql_query($sql) or die('数据库查询失败!错误原因:'.mysql_error());
?></code>
Copy after login
Copy after login

login.php

<code><?php header('Content-Type:application/json;charset=utf-8');
        //判断请求类型
        if (isset($_POST['phone']) && isset($_POST['password'])) {
            // 获取POST请求参数
            $accessType = '[POST]';
            $userPhone = $_POST['phone'];
            $userPassword = $_POST['password'];
        } else {
            echo json_encode(array('result'=>'非法请求'));
            return false;
        }
        
        //创建SQL语句
        $sql="SELECT * FROM lms_users WHERE phone ='".$userName."' AND `password` = '".$userPassword."';";
    
        //调用conn.php文件进行数据库操作
        require('Conn.php');
        global $conn;
        
        $result=mysql_query($sql,$conn) or die('数据库查询失败!错误原因:'.mysql_error());
        
        $flag=0;
        while(!!$item=mysql_fetch_assoc($result)){
            $flag++;
        };
        //实际只能返回一条结果.
        if ($flag >= 1) {
            $result = array(
                            echo json_encode($row);
                           );
            echo json_encode($result);
            exit();
        }else {
            $result = array(
                            'id' => 0
                            );
            echo json_encode($result);
        }
        return true;
?></code>
Copy after login
Copy after login

login.html

<code>



    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>PHP 测试</title>


    
<p></p>
<h1>登陆</h1>
<hr>
<form action="login.php" method="post">
    <p>姓名: <input type="text" name="phone"></p>
    <p>密码: <input type="password" name="password"></p>
    <p><input type="submit"></p>
</form>


</code>
Copy after login
Copy after login

开发的话要打开php的错误显示(在php.ini中搜索配置display_errors=On),才能知道具体是啥错误。

另外,建议不要使用mysql相关函数了,用mysqli相关函数或对象代替,mysql相关函数早就不推荐使用,并在最新的PHP7中正式移除了。

有语法错误吧,

<code>$result = array(
    echo json_encode($row);
);
                       </code>
Copy after login

这里是错误的。其实可以打开服务器上详细错误的输出,这样可以看到500错误具体的出错原因

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template