我一直在試圖準備一個查詢,然後將它發送到數據庫,但它沒有從數據庫中取出任何東西。它告訴我用戶/傳球組合不正確。 (當它是正確的),這意味着它有來自數據庫的0行。任何人都可以告訴我如何解決這個問題?PHP準備查詢不起作用
/* Create a prepared statement */
$stmt = mysqli_stmt_init($con);
mysqli_stmt_prepare($stmt, "SELECT * FROM users WHERE username = ? AND userpass = ?");
/* Bind parameters
s - string, b - blob, i - int, etc */
mysqli_stmt_bind_param($stmt, "ss", $username, $userpass);
/* Execute it */
mysqli_stmt_execute($stmt);
/* Bind results */
mysqli_stmt_bind_result($stmt, $username, $userpass);
/* Fetch the value */
mysqli_stmt_fetch($stmt);
/* close statement */
mysqli_stmt_close($stmt);
/* $result = mysqli_query($con,"SELECT * FROM users
WHERE username = '$username' AND userpass = '$userpass'"); */
if(!$stmt)
{
//something went wrong, display the error
echo '<ul class="ulstylecenter">
<li>Something went wrong while signing in. Please try again later.</li>
<li>If you are not redirected in 5 seconds please <a href="/home.php">click here</a>.</li>
</ul>';
header('Refresh: 5;url=/home.php');
//echo mysqli_error(); //debugging purposes, uncomment when needed
}
else
{
//the query was successfully executed, there are 2 possibilities
//1. the query returned data, the user can be signed in
//2. the query returned an empty result set, the credentials were wrong
if(mysqli_num_rows($stmt) == 0)
{
echo '<ul class="ulstylecenter">
<li>You have supplied a wrong user/password combination. Please try again.</li></ul>';
echo '<form method="post" action="">
<table>
<tr>
<th><label for="username" class="signinlabel">Username:</label></th>
<td><input type="text" name="username" value="';
if(isset($username)){ echo $username; }
echo '" class="signininput"></td>
</tr>
<tr>
<th><label for="userpass" class="signinlabel">Password:</label></th>
<td><input type="password" name="userpass" class="signininput"></td>
</tr>
</table>
<ul class="forgotsignin"><li><a href="#">Forgot your username or password?</a></li></ul>
<input type="submit" value="Sign In" class="signinbutton" id="signinbuttonid">
</form>';
}
else
{
$_SESSION['signed_in'] = true;
//we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages
while($row = mysqli_fetch_assoc($stmt))
{
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['username'] = $row['username'];
$_SESSION['useremail'] = $row['useremail'];
$_SESSION['userdate'] = $row['userdate'];
}
echo '<ul class="ulstylecenter">
<li>Succesfully logged in.</li>
<li>If you are not redirected in 5 seconds please <a href="/home.php">click here</a>.
</li></ul>';
header('Refresh: 5;url=/home.php');
}
你應該添加錯誤處理您的數據庫調用開始。 – jeroen
您的密碼是以純文本還是以某種形式存儲的? –
「它告訴我」 - 請提供錯誤報告,而不是您的解釋。同時將您的代碼修剪到相關部分。 – symcbean