2016-03-25 135 views
1

我有一个名为学校的MySQL数据库是这样设置的: schoolID(1),schoolName(学校1),schoolCounty(白金汉郡),schoolUsername(school1admin), schoolPassword(school1password)使用PHP来匹配用户名和密码与HTML下拉

我现在有一个下拉菜单,显示的学校名单,当我输入任何用户名和密码进入HTML登录表单我都可以登录。

我似乎无法正常工作出我如何设置它,取决于学校的选择取决于使用的用户名和密码。

例如,如果我选择school1,那么我只能使用school1的用户名和密码。

这是我到目前为止的index.php文件:

<?php 

require_once 'databaseConnect.php'; // connects to the databse via this file 
if ($conn->connect_error) die ($conn->connect_error); // check the connection to the database. If failed, display error 


$sql = "SELECT * FROM school"; 
$result = $conn->query($sql); 


$conn->close(); 
?> 


<html> 
    <body> 
     <title>EduKode</title> 




     <div id="login-form-container"> 
      <p>Log In:</p> 

<?php 
echo'<div id="schoolSelection">'; 
echo '<select name="schoolName">'; 
if ($result->num_rows > 0) { 
// output data of each row 
while($row = $result->fetch_assoc()) { 
echo '<option>'. $row["schoolName"]. "<br>"; 
} 
} else { 
echo "0 results"; 
} 
echo '</select>'; 

echo'</div>'; 

//http://stackoverflow.com/questions/10009464/fetching-data-from-mysql-database-to-html-dropdown-list 
?> 

       <form id="login-form" name="contactform" method="post" action="checkSchoolCredentials.php"> <!-- when submitting the form will call the 'authenticate.php' script.--> 
        <div class="contact-form"> 

         <label>Username:</label> 
         <input name="username" type="text"> <!-- students created username field--> 

         <label>Password:</label> 
         <input name="password" type="password"> <!-- students created password field--> 
        </div> 
         <div id="submit-button"> 
          <input type="submit" name="submit" value="Log In"> 
         </div> 
       </form> 
     </div> 

    </body> 
</html> 

这是checkSchoolCredentials.php:

<?php 
require_once 'databaseConnect.php'; // connects to the databse via this file 
if ($conn->connect_error) die ($conn->connect_error); // check the connection to the database. If failed, display error 


if(isset($_POST['submit'])) // if submit button is pressed 
{ 
    $username = $_POST['username']; //assigns the value of the input box username to $username 
    $password = $_POST['password']; //assigns the value of the input box password to $password 

    $query = "SELECT * FROM school WHERE schoolUsername='$username' AND schoolPassword ='$password'"; // Query the database 


    $result=mysqli_query($conn, $query); 
    if(mysqli_num_rows($result) ==1) 
    { 
     session_start(); // start session 
     $_SESSION['auth']='true'; 
     $_SESSION['username'] = $username; // save session as username 
     header('location:taskSelection.php'); // if correct, redirect to taskSelection.php 

    } 
    else 
    { 
     header('location:index.php'); // redirect to index.html if incorrect 


    } 

} 


$conn->close(); 

?> 
+0

尝试改变'回声 '

+0

时发送给服务器的东西哦,并且放在'

'元素,否则在提交表单时不会提交给服务器 –

回答

-1

你接近,你有什么是也送schoolname并检查是否所有的变量设置:

if (isset($POST['username'],$POST['userpassword'],$POST['schoolName'])

然后只需更换:

$query = "SELECT * FROM school WHERE schoolUsername='$username' AND schoolPassword ='$password'"; // Query the database 

有:

$query = "SELECT * FROM school WHERE schoolUsername='$username' AND schoolPassword ='$password' AND schoolName='$schoolName'"; // 

现在你要知道现在我的查询仍然很糟糕,因为它容易受到SQL注入。你必须使用prepare statements instead

$sql = "SELECT * FROM school WHERE schoolUsername=? AND schoolPassword = ? AND schoolName=?"; 
if ($query = $conn->prepare($sql)){ 
    $query->bind_param("s", $username,$password,$schoolName); 
    $stmt->bind_result($result); 
    while($stmt->fetch()){ 
    // you can work with $result which is an array containing a line of the results 
    } 
+0

非常感谢您的帮助!非常有帮助。它似乎工作到目前为止。你是否说这种方法对恶意用户开放? – Toby

+0

肯定是的。任何知道sql并为sql注入自己的人都可以执行这些攻击(http://www.w3schools.com/sql/sql_injection.asp)。这就是为什么你需要使用准备声明。我编辑了我的答案。 – rsabir

+0

我对PHP不太好。我有2个错误“注意:未定义的变量:dbh和致命错误:调用成员函数prepare()null – Toby

-1

添加AND schoolName = "$schoolName"到您的SQL语句