查询数据库时收到错误

时间:2014-02-15 15:14:28

标签: php mysql html5 twitter-bootstrap-3 wamp

我正在尝试为我的网站创建一个登录表单,其中用户名和密码已存储在mydb中的表用户中。我创建了checklogin.php来创建从我的网站到mydb的连接,然后检查登录的凭据。

这是checklogin.php的代码:

<?php
$host="127.0.0.1:3306"; // host name
$username="root"; // mysql usrname
$password=""; // mysql password
$db_name="mydb"; // db name
$tbl_name="user"; // table name

//Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die(mysql_error());

//User name and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

if($count==1)
{   // register $myusername, $mypassword and redirect file to loginSuc.php
    session_register("myusername");
    session_register("mypassword");
    header("location:loginSuc.php");
}
else 
{
    echo "wrong username or password";
}
?>

现在我已将缺少的组件添加到上一代码中,当我运行时,我现在遇到指向第13,14和19行的错误。

错误是:

  

注意:未定义的索引:C:\ wamp \ www \ HTML 5中的myusername,CSS 3&amp;第13/14/19行的PHP \ checklogin.php,字符串“错误的用户名或密码”出现在

下面

再次坚持如何解决这个问题 - 修复

通过添加一些额外的代码修复了第13,14和19行的错误。现在有错误:

  

“解析错误:语法错误,C:\ wamp \ www \ HTML 5中的意外T_IF,第3行的CSS 3&amp; PHP \ checklogin.php”

<?php
$host="127.0.0.1:3306"; // host name
$username="root"; // mysql usrname
$password=""; // mysql password
$db_name="mydb"; // db name
$tbl_name="user"; // table name

//Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die(mysql_error());

//User name and password sent from form
$myusername=$_POST["myusername"];
$mypassword=$_POST["mypassword"];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result)

if($count==1)
{   // register $myusername, $mypassword and redirect file to loginSuc.php
    session_register("myusername");
    session_register("mypassword");
    header("location:loginSuc.php");
}
else 
{
    echo "wrong username or password";
}
?>

上面已经固定

对,所以现在我已经使用了checkilogin.php,现在使用:

<?php

$host="127.0.0.1:3306"; // host name
$username="root"; // mysql usrname
$password=""; // mysql password
$db_name="mydb"; // db name
$tbl_name="user"; // table name

//Connect to server and select databasemysql_connect
mysql_connect($host, $username, $password, $db_name) or die(mysql_error());

if($_POST)
{   
    $myusername=$_POST["myusername"];
    $mypassword=$_POST["mypassword"];

    $sql="SELECT * FROM ".$tbl_name." WHERE myusername=".$myusername." and mypassword=".$mypassword;
    $result=mysql_query($sql);

    // register $myusername, $mypassword and redirect file to loginSuc.php
    session_register("myusername");
    session_register("mypassword");
    header("location:loginSuc.php");

}
else 
{
    echo "wrong username or password";
}

?>

但我仍然遇到问题。每当我尝试通过loginpage.html中的表单登录时,都会转到checklogin.php,但checklogin.php只显示“密码或用户名错误”。当我按下没有用户名或密码的提交按钮,当我输入错误的用户名和密码,甚至当我使用我在mydb的用户表中作为示例的用户名和密码时,我得到了这个。 (在用户表中,我有用户ID,用户名和密码字段)。

以下是我在loginpage.html开头的php:

<?php 
session_start();
if(!session_is_registered(myusername))
{
header("location:checklogin.php");
}
?> 

这里是表格(我使用的是html5和twitter bootstrap网格代码):

    

<form class="form-horizontal" method="post" action="checklogin.php">
    <div class="form-group">
        <label for="myusername" class="control-label col-xs-3">Username</label>
        <div class="col-xs-6">
            <input type="text" class="form-control" id="myusername" placeholder="Library card number">
        </div>
    </div>
    <div class="form-group">
        <label for="mypassword" class="control-label col-xs-3">Password</label>
        <div class="col-xs-6">
            <input type="password" class="form-control" id="mypassword" placeholder="Password">
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-6">
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-offset-2 col-xs-24">
            <button type="submit" class="btn btn-primary">Login</button>
        </div>
    </div>
</form> 

3 个答案:

答案 0 :(得分:1)

您应该使用mysql_select_db的变量名而不是文字字符串。

$host="127.0.0.1:3306"; // host name
$username="root"; // mysql usrname
$password=""; // mysql password
$db_name="mydb"; // db name
$tbl_name="user"; // table name

//Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db($db_name)or die(mysql_error()); // <-- $db_name instead of 'db_name'

//User name and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

if($count==1)
{   // register $myusername, $mypassword and redirect file to loginSuc.php
    session_register("myusername");
    session_register("mypassword");
    header("location:loginSuc.php");
}
else 
{
    echo "wrong username or password";
}

答案 1 :(得分:0)

此处db_name应为$db_name

 mysql_select_db($db_name)or die(mysql_error());

答案 2 :(得分:0)

我认为这应该有用

<?php
$host="127.0.0.1:3306"; // host name
$username="root"; // mysql usrname
$password=""; // mysql password
$db_name="mydb"; // db name
$tbl_name="user"; // table name

//Connect to server and select databasemysql_connect
mysql_connect($host, $username, $password, $db_name) or die(mysql_error());

//execute when form is submit
if($_POST)
{   //set send data into variables
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];

//check if username have been set
    if(is_null($myusername) || empty($myusername))
    {  $alert = "Enter something!";
    }
    else if(isset($myusername))
    {    $sql="SELECT * FROM ".$tbl_name." WHERE username=".$myusername." and password=".$mypassword;
        $result=mysql_query($sql);

    //convert data into array
        $data = mysql_fetch_array($result);
    //check if array is not empty
        if($data)
        {   //goto this line if user exist
            //set or register `$myusername` to variable `username` in session
            $_SESSION['myusername']=$myusername;

            header("location:loginSuc.php");
        }
        else 
        {   $alert = "wrong username or password"; }
    }
}
?>

您实际上可以使用$_SESSION['myusername']$_SESSION['mypassword']来使用会话创建变量。 - https://stackoverflow.com/a/3682629/3306059

还为这些输入命名(与php代码中的名称相同),以便您可以使用$_POST

来调用它们
<form class="form-horizontal" method="post" action="checklogin.php">
    <div class="alert">
    <?php 
        if(isset($alert))
        {   echo $alert; }
    ?>
    </div>
    <div class="form-group">
        <label for="myusername" class="control-label col-xs-3">Username</label>
        <div class="col-xs-6">
            <input type="text" class="form-control" id="myusername" placeholder="Library card number" name="myusername">
        </div>
    </div>
    <div class="form-group">
        <label for="mypassword" class="control-label col-xs-3">Password</label>
        <div class="col-xs-6">
            <input type="password" class="form-control" id="mypassword" placeholder="Password" name="mypassword">
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-6">
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-offset-2 col-xs-24">
            <button type="submit" class="btn btn-primary">Login</button>
        </div>
    </div>
</form>