首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我无法将PHP变量传递给SQL查询

问我无法将PHP变量传递给SQL查询
EN

Stack Overflow用户
提问于 2015-09-13 09:36:28
回答 3查看 169关注 0票数 2

我的密码在下面。我尝试使用PHP创建一个登录表单。它不起作用,所以请纠正我。

代码语言:javascript
复制
<?php
    $mycon = mysql_connect('localhost', 'root', '');
    $db = mysql_select_db('data', $mycon);

    $_user = mysql_real_escape_string($_POST['user']);
    $_pass = mysql_real_escape_string($_POST['pass']);

    $qur = "select * from admin where username = '$_user' and password = '$_pass' ";

    $res = mysql_query($qur, $mycon);

    if (mysql_num_rows($res) > 0) {

    }
    else {
        header('Location:admin.php');
    }
?>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-09-13 10:03:21

将<form>与方法POST与隐藏字段一起使用,如:

代码语言:javascript
复制
<?php
    if (isset($_POST['check'])) {
        $mycon = mysql_connect('localhost', 'root', '');
        $db = mysql_select_db('data', $mycon);

        $_user = mysql_real_escape_string($_POST['user']);
        $_pass = mysql_real_escape_string($_POST['pass']);

        $qur = "SELECT * from admin where username = '$_user' AND password = '$_pass' ";

        $res = mysql_query($qur, $mycon);

        if (mysql_num_rows($res) > 0) {

            //Your stuff here

        }
        else {
            header('Location:admin.php');
        }
    }
?>

<form role="form" method = "POST" action = "">

    Username:
        <input type = "text" name = "user">
    Password:
        <input type = "text"   name = "pass">
        <input type = "hidden" name = "check">
        <input type = "submit" name = "submit" value="submit">
</form>

不推荐使用MySQL函数。请切换到http://en.wikipedia.org/wiki/MySQLi或https://en.wikipedia.org/wiki/PHP#Development_and_community。

票数 4
EN

Stack Overflow用户

发布于 2015-09-13 09:56:29

​

代码语言:javascript
复制
<?php
    $mycon = mysql_connect('localhost', 'root', '');
    $db = mysql_select_db('data', $mycon);

    $_user = mysql_real_escape_string($_POST['user']);
    $_pass = mysql_real_escape_string($_POST['pass']);

    $qur = "select * from admin where username = '".$_user."' and password = '".$_pass."'";

    $res = mysql_query($qur, $mycon);

    if (mysql_num_rows($res) > 0) {

    }
    else {
        header('Location:admin.php');
    }
?>

​

票数 -1
EN

Stack Overflow用户

发布于 2015-09-13 09:57:54

​

代码语言:javascript
复制
<?php
    $mycon = mysql_connect('localhost', 'root', '');
    $db = mysql_select_db('data', $mycon);

    $_user = mysql_real_escape_string($_POST['user']);

    $_pass = mysql_real_escape_string($_POST['pass']);


    $qur = "select * from `admin` where `username`='{$_user}' and `password` ='{$_pass}'";


    $res = mysql_query($qur, $mycon);

    if (mysql_num_rows($res) > 0) {

        //Here your code will work

    }
    else {

        //And here it will not work
        header('Location:admin.php');
    }
?>

​

不要忘记检查表的列名。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32548283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档