0

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

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

问题是:

$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>
4

2 回答 2

1

使用此查询。

$query = mysql_query ("SELECT * from customer where username = '".$_SESSION['myusername']."'");
于 2013-05-06T09:37:04.637 回答
0
//$query = mysql_query ("SELECT * from customer where username = $_SESSION['myusername']");

尝试这个

$query = mysql_query ("SELECT * from customer where username = '$_SESSION['myusername']'");
于 2013-05-06T09:36:48.650 回答