我正在尝试在为登录系统提供管理员用户名和密码时打开admin.php页面。这是我到目前为止将用户重定向到主页的内容,但我不确定我的代码是否正确。我尝试过使用if语句,但我不知道我是否将其合并。我的数据库是用mySQL编写的,管理员登录名和密码存储在数据库的插槽“1”中。我是PHP和所有mySQL的新手。
修改
idClients
是特定ID的数据库列。
这是我的代码:
<?php
$hostname = 'hostname';
$dbname = 'dbname';
$username = 'username';
$password = 'password';
$con=mysql_connect($hostname,$username,$password) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db($dbname,$con) or die("Failed to connect to MySQL: " . mysql_error());
$myusername = $_POST['username'];
$mypassword = $_POST['password'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$query = "SELECT * FROM Client_Information WHERE Username='$myusername' and Password='$mypassword'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
mysql_close();
if($count==1){
$seconds = 5 + time();
setcookie(loggedin, date("F jS - g:i a"), $seconds);
header("location: login_success.php");
这是我试图合并的if语句:
if ($myusername['idClients'] == '#') {
header("Location: admin.php");
}
然后在'if'语句之前的代码所需的else语句。
}else{
echo '<div class="incorrect">- Incorrect username or password -</div>';
}
?>
非常感谢所有帮助:)
答案 0 :(得分:1)
编写如下代码: -
$query = "SELECT * FROM Client_Information WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($query);
if (!$result) {
// Debug query result by below code
//echo 'Could not run query: ' . mysql_error();
//exit;
echo '<script language="javascript">';
echo 'alert("Username / Password Seems Wrong !")';
echo '</script>';
// OR
// echo '<div class="incorrect">- Incorrect username or password -</div>';
}else{
$row = mysql_fetch_row($result);
$idClientValue = $row[0];
if ($idClientValue == '43') {
header("Location: admin.php");
}
$seconds = 5 + time();
setcookie(loggedin, date("F jS - g:i a"), $seconds);
header("location: login_success.php");
}
答案 1 :(得分:0)
使用脚本而不是标题。
if(true)
{
echo "<script>
window.location = 'index.php?q=success';
</script>";
}else{
echo "<script>
window.location = 'index.php?q=failure';
</script>";
}
答案 2 :(得分:0)
<?php
$hostname = 'hostname'; / Write Here Your Hostname For Localhost it is 'localhost'
$dbname = 'dbname'; / Write Here Your Database Name like 'testdb'
$username = 'username'; / Write Here Username For Localhost it is 'root'
$password = 'password'; / Write Here Password of DB for localhost it is Blank ''
$con=mysql_connect($hostname,$username,$password) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db($dbname,$con) or die("Failed to connect to MySQL: " . mysql_error());
$myusername = $_POST['username'];
$mypassword = $_POST['password'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$query = "SELECT * FROM Client_Information WHERE Username='$myusername' and Password='$mypassword'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
mysql_close();
if($count==1){
$seconds = 5 + time();
setcookie(loggedin, date("F jS - g:i a"), $seconds);
header("location: login_success.php"); // Here is the Redirection Page.
}
?>
您的准则是正确的,但您没有填写您的准则中所需的信息。完成该信息&amp;再跑一次