dongming4994 2013-05-06 09:33
浏览 52

使用$ _session选择数据

I have constructed a small script for a coursework for school which will update a users details in a database.

My update script works however what I would like to achieve is that data is selected using a session variable in php.

The line is question is:

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

I would prefer it to read something like:

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

But this doesn't work. The session is passed from my login form and can display the username on the page but doesn't work in my select statement.

Here is my code:

<?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条回答 默认 最新

  • dounianxie2058 2013-05-06 09:36
    关注
    //$query = mysql_query ("SELECT * from customer where username = $_SESSION['myusername']");
    

    try this

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b