0

我正在努力从我的数据库中进行选择并将其添加到 a$_SESSION以继续到个人资料页面。
请注意,“ $_SESSION['user'] = $email;”在个人资料上有效并打印,但我确信那是因为它已经提出了我的表格。

连接.php

<?php

session_start();

$required = array('email', 'password');

// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}

if ($error) {
die("All fields are required.");
} else {

//CHECK POINT 1

$con = mysql_connect("**HOST**", "**DB**", "**PASSWORD**");

mysql_select_db("**DB**")or die("cannot select DB");

// username and password sent from form 
$email=$_POST['email']; 
$psswrd=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection)
$email = stripslashes($email);
$psswrd = stripslashes($psswrd);
$email = mysql_real_escape_string($email);
$psswrd = mysql_real_escape_string($psswrd);
$sql="SELECT * FROM **Table WHERE email='$email' and password='$psswrd'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

//fetch info & create session
$id="SELECT 'id' FROM **table**";
$_SESSION['user'] = $email;
$_SESSION['uid'] = $id;
$_SESSION['name1'] = $fname;  

配置文件.php

<?php

session_start();
$email = $_SESSION["user"];
$id = $_SESSION["uid"];
$fname = $_SESSION["name1"];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>...</head>
<body>
<div align=center><font color="#000000" face="Bodoni MT" class="ws12"><?php echo "I am ". $id. ". Contact me at ". $email ?></font></div>
</body>
</html>
4

2 回答 2

0

mysql_query 将返回一个结果集。您可以使用 mysql_fetch_assoc 将返回的数据放入一个数组中。正如索利莫西建议的那样。

所以这将是:

$user_data_array = mysql_fetch_assoc($result);

然后您可以将这些值添加到会话数据中:

$_SESSION['user'] = $user_data_array["email"];
$_SESSION['uid'] = $user_data_array["id"];
$_SESSION['name1'] = $user_data_array["fname"];

如果这些列是正确的。

于 2013-11-04T16:37:32.087 回答
0

您必须从数据库中获取数据(“$result”变量)。尝试这个:

<?php

session_start();

$required = array('email', 'password');

foreach ($required as $field) {
    if (empty($_POST[$field])) {
        $error = true;
    } else {
        $error = false;
    }
}

if (true === $error) {
    die("All fields are required.");
} else {
    $con = mysql_connect("**HOST**", "**DB**", "**PASSWORD**");
    mysql_select_db("**DB**") or die ("cannot select DB");

    $email  = $_POST['email']; 
    $psswrd = $_POST['password']; 
    $email  = stripslashes($email);
    $psswrd = stripslashes($psswrd);
    $email  = mysql_real_escape_string($email);
    $psswrd = mysql_real_escape_string($psswrd);
    $result = mysql_fetch_assoc(mysql_query("SELECT * FROM **Table WHERE email='$email' and password='$psswrd'"));

    if (!empty($result)) {
        $_SESSION['user']  = $result['email'];
        $_SESSION['uid']   = $result['id'];   // id column name in your database
        $_SESSION['name1'] = $result['name']; // name column name in your database
    }
于 2013-11-04T16:42:19.647 回答