PHP + MySQL - 不会从数据库中读取

时间:2014-05-03 03:00:56

标签: php mysql

我正在为学校制作一个需要登录页面的网站项目。我已经将PHP设置为写入数据库,这没有问题。我的新问题是实际上没有任何东西会从数据库中读取。我在我的localhost上测试,连接很好。我收到的错误是电子邮件不正确,实际上它是有效的。希望你们能为我解决一个简单的问题!谢谢!

PHP:

<?php

//Setup our DB connection
include('../../connect/local-connect.php');

// Get the user entered elements
$email = $_POST['email'];
$pword = $_POST['password'];

// email query
$query = "SELECT email FROM project WHERE email = '$email'";

//Run email query
$result = mysqli_query($dbc, $query) or die('Read error - email');

// # of results
if (mysqli_num_rows($result) == 0)
{
    header('Location:login.php?logerror=1');
    exit;
}

//If we got here, we have validated the email

// Build the username query
$query = "SELECT * FROM project WHERE email = '$email' AND password = '$pword'";

// Run the password query
$result = mysqli_query($dbc, $query) or die('Read error - password');

// Did we get a row?
if (mysqli_num_rows($result) == 0)
{
    header('Location:login.php?logerror=2');
    exit;
}

// If we got here, we have validated an email and password combo.

//  Close the DB connection
mysqli_close ($dbc);

// Start a PHP session
session_start();

//Get and store our session variables
$row = mysqli_fetch_array($result);
$_SESSION['sname']=$row['name'];
header('Location:custwelcome.php');
exit;

// Pass a '3' back to login.php for testing (Keep this code at the bottom!)
header('Location:login.php?logerror=3');
exit;

    ?>

HTML:

<!DOCTYPE html>

<!-- 
XXXX 
login.php 
-->

<?php
    //  Start a PHP session
    session_start();

    // Check to see if user is already logged in
    if(isset($_SESSION["sname"]))
    {
        header('Location:custwelcome.php');
        exit;
    }

?>

<html lang="en">

  <head>
    <!-- Meta tag -->
    <meta name="robots" content="noindex.nofollow" />
    <meta charset="utf-8" />

    <!-- Link tag for CSS -->
    <link type="text/css" rel="stylesheet" href="../stylesheet/project.css" />

    <!-- Javascript tags -->
    <script type="text/javascript" src="../js/messages.js"></script>


    <!-- Web Page Title -->
    <title>Login Page</title>

  </head>

<body>
    <div id="header">

                <img src="../images/logo.png" alt="Logo" />

        <p class="sh1">Shoe Source Unlimited</p>
        <p class="sh2">Your source for lightning sales of this season's hot shoes!    </p>

        <p class="sh3">LeAndra Marx</p>
  </div>

    <div id="navbar">
        <ul id="nav">
            <li>
                <a href="../homepage.htm">Home</a>                          
             </li>

            <li>
                <a href="#">Men's</a>
                    <ul>
                        <li><a href="../sneakers.htm">Sneakers</a></li>
                         <li><a href="../loafers.htm">Loafers</a></li>
                        <li><a href="../athletic.htm">Athletic</a></li>
                    </ul>                       
            </li>

             <li>
                <a href="#">Women's</a>
                    <ul>
                        <li><a href="../boots.htm">Boots</a></li>
                        <li><a href="../heels.htm">Heels</a></li>
                            <li><a href="../sandals.htm">Sandals</a></li>       
                    </ul>               
            </li>

            <li>
                <a href="../about.htm">About Us</a>         

            </li>

            <li>
                <a href="../signup.htm">Sign Up</a>

            </li>

            <li>
                <a href="login.php">Log In</a>

            </li>

        </ul>   
    </div>





  <div id="external">

    <p>

            <a href="https://twitter.com/XXXXX"     onclick="window.open(this.href); return false;">
                <img src="../images/twitter.jpg" alt="twitter" />
            </a>
    </p>
            <p>Follow us on Twitter!</p>
        <br/>


    <p>

            <a href="http://www.facebook.com/ShoeSourceUnlimited" onclick="window.open(this.href); return false;">
                <img src="../images/facebook.png" alt="facebook" />
            </a>
    </p>
            <p>Like us on Facebook!</p>
        <br/>


         <a href="email/projectem.htm">
        <img src="../images/email.jpg" alt="pinkemail"  />
        </a>

            <p> Send us an email!</p>   
     </div> 


    <div id="main">
        <p>Sign in below to start making purchases!</p>

    </div>


    <p id="registered"> Don't have an account? Sign up <a     href="../signup.htm">here</a>!
    <div id="userform">
        <p class="fh">Log In Here</p>

        <form id="loginform" action="logincheck.php" method="post">

                <?php
                // Check return codes from logincheck.php
                if ($_GET["logerror"] == 1)
                {
                    echo '<p class="loginerror">Invalid Email!</p>';
                }
                if ($_GET["logerror"] == 2)
                {
                    echo '<p class="loginerror">Invalid Password!</p>';
                }
                if ($_GET["logerror"] == 3)
                {
                    echo '<p class="loginerror">Returned from     logincheck</p>';
                }
            ?>

            <p>

                <!--Email -->
                <label for="email">Email:</label>
                <input type="email" id="email" name="email" required autofocus
                title="Email: 6-59 characters, lowercase, valid email only!"
                pattern="[a-z0-9.-_]+@[a-z0-9-]+\.[a-z]{2,6}"
                maxlength="60"
                onfocus="emailmsg()" />
                <br />

                <!--Password -->
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" required
                title="Password: 5-15 characters, U/L, 0-9, . - _ ! $ only!"
                pattern="[a-zA-Z0-9.-_!$]{5,15}" 
                onchange="form.reenter.pattern=this.value;"
                onfocus="passmsg()"/>
                <br />


 <!-- Build buttons so that they are consistent throughout the site 
                never use get method for confidential data-->
            <p class="submit">
                <input type="submit" 
                value=" Log In! "
                onfocus="signmsg()"/> 


                <span class="reset">

                    <input type="reset" value=" Clear "     onclick="history.go(0)" 
                    onfocus="clearmsg()"/>
                </span>
            </p>

        </form>

    </div>

    <div id="messages">
    <p></p>
    </div>

    <div id="footer">
        <p>

            &copy;2014, XXXX

        </p>
    </div>

  </body>

</html>

如果需要查看更多文件,请告诉我,我只想知道这两个文件存在的问题。

我的解决方案:我所做的就是更改我的表名,它开始工作了。我觉得这很好!感谢所有试图提供帮助的人:)

2 个答案:

答案 0 :(得分:0)

你的数据库连接好吗?首先检查连接是否完美。

在数据库连接文件的顶部,请在错误报告上找出错误

error_reporing(E_ALL);
ini_set('display_errors',1);

并且在数据库连接之后使用以下语法..如果连接失败,您将获得所有

mysql_connect('host','username','password') OR die(mysql_error());
mysql_select_db('dbName') OR die(mysql_error());

答案 1 :(得分:0)

<?php

$con=mysqli_connect("host","username","password","my_db");


if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

for mysqli