使用$ _session选择数据

时间:2013-05-06 09:33:31

标签: php mysql sql

我为学校的课程作了一个小脚本,它将更新数据库中的用户详细信息。

我的更新脚本有效,但我想要实现的是使用php中的会话变量选择数据。

问题是:

$query = mysql_query ("SELECT * from customer where username = 'ryan'");

我希望它能读出类似的内容:

$query = mysql_query ("SELECT * from customer where username = $_SESSION['myusername']");

但这不起作用。会话从我的登录表单传递,可以在页面上显示用户名,但在我的select语句中不起作用。

这是我的代码:

<?php
session_start();

$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("ryandb", $con);

echo "welcome ". $_SESSION['myusername'] . " to the members only page</br>";


$query = mysql_query ("SELECT * from customer where username = 'ryan'");  


while ($row = mysql_fetch_array ($query)) 
{
  $username = $row['username'];
  $firstname = $row['firstname'];
  $surname = $row['surname'];
  $email = $row['email'];
}

?>

<form method="post" action="updateuser.php">
Username: <input type="text" name="username" value="<?php echo $_SESSION['myusername']; ?    >" /><br />
Firstname: <input type="text" name="firstname" value="<?php echo $firstname; ?>"/><br />
Surname: <input type="text" name="surname" value="<?php echo $surname; ?>"/><br />
Email: <input type="text" name="email" value="<?php echo $email; ?>"/><br />
<input type= "submit" name = "update" value="Update">
</form>

2 个答案:

答案 0 :(得分:1)

使用此查询。

$query = mysql_query ("SELECT * from customer where username = '".$_SESSION['myusername']."'");

答案 1 :(得分:0)

//$query = mysql_query ("SELECT * from customer where username = $_SESSION['myusername']");

试试这个

$query = mysql_query ("SELECT * from customer where username = '$_SESSION['myusername']'");