我已经获得了一段代码来恢复用户名和密码,看看它们是否匹配。我现在想要设置一个会话变量到我在表中的'points'值,该值与用户名在同一行并传递..可以做什么?
<?php $username="asdin";
$password="1sdA2";
$database="a75sdting";
$pword = $_REQUEST['pword'];
$uname = $_REQUEST['uname'];
mysql_connect('mysqsdst.com',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = mysql_query("SELECT * FROM `username` WHERE `password` = '$pword' AND `username` = '$uname'");
$exsists = 0;
WHILE($rows = mysql_fetch_array($query)){
$exsists = 1;
break;
}
if ($exsists){
$_SESSION['usern']=$uname;
$_SESSION['logged']=1;
header('Location: http://wwsdipts/logged2.php');
}
mysql_close();
?>
我想设置$ _SESSION ['points'] = $ row [points]我猜......但我不认为这是正确的
答案 0 :(得分:1)
<?php
// start session (required on every page that uses sessions
session_start();
// db auth
$username="asdin";
$password="1sdA2";
$database="a75sdting";
// user auth
$pword = $_POST['pword']; // should use either $_POST or $_GET, NOT $_REQUEST
$uname = $_POST['uname']; // should use either $_POST or $_GET, NOT $_REQUEST
// open db connection
$conn = mysql_connect('mysqsdst.com',$username,$password);
@mysql_select_db($database,$conn) or die( "Unable to select database");
// check user
$query = mysql_query("SELECT * FROM `username` WHERE `password` = '$pword' AND `username` = '$uname'");
if(mysql_num_rows($query)){
// user exists
$row = mysql_fetch_assoc($query);
$_SESSION['usern']=$uname;
$_SESSION['logged']=1;
header('Location: http://wwsdipts/logged2.php');
}else{
header('Location: http://wwsdipts/login.php'); // take them back to login page if incorrect details
}
// close db connection
mysql_close($conn);
?>
我已经整理了一下你的代码,请看一下这些笔记。它也是值得的:
mysql_real_escape_string($_POST['uname'])
- 密码相同username
的桌子吗?您应该阅读一些关于数据库设计的信息,这个表的更好的名称/用途将是用户,因为该表将持有用户(唯一ID,用户名和密码的组合。$row['column-name']
)后使用$row = mysql_fetch_assoc($query);
答案 1 :(得分:0)
你是对的,但在这种情况下你的数组是rows
,它应该在
$_SESSION['points'] = $rows['points']
它应该在你的while
循环中:
WHILE($rows = mysql_fetch_array($query)){
$exsists = 1;
$_SESSION['points'] = $rows['points']
break;
}
然而,做这样的事情可能会更好:
if(mysql_num_rows($result) == 1) {
//Login Successful
rows = mysql_fetch_assoc($result);
$_SESSION['points'] = $rows['points']
$_SESSION['usern']=$uname;
$_SESSION['logged']=1;
header('Location: http://wwsdipts/logged2.php');
}