dsxrq28228 2015-03-26 18:12
浏览 38

PHP MySQL会话连接数据库

This is a long question so apologies. I have the following code.

 $register="SELECT * from register WHERE username = '". $_SESSION['username']."'";

$re = $connect->query($register);

$numrow = $re->num_rows; 

echo "<table border='1'>";
echo "<tr>";
echo"<th>Username</th>";
echo"<th>Forename</th>";
echo"<th>Surname</th>";
echo"<th>Course</th>";
echo"<th>Subject</th>";
echo"<th>Level</th>";
echo"<th>Date</th>";
echo"<th>Time</th>";
echo"</tr>";

$count = 0;
while ($count < $numrow) 
{
$row = $re->fetch_assoc();
extract($row);    
echo "<tr>";

echo "<td>";
echo $username;
echo "</td>";

 echo "<td>";
 echo $firstName;
 echo "</td>";

 echo "<td>";
 echo $surname;
 echo "</td>";

 echo "<td>";
 echo $course;
 echo "</td>";

 echo "<td>";
 echo $subject;
 echo "</td>";

  echo "<td>";
   echo $level;
 echo "</td>";

 echo "<td>";
 echo $date;
 echo "</td>";

 echo "<td>";
 echo $time;
 echo "</td>";

 $count = $count + 1;

 }
 ?>

The idea is that the page gets the session username (the user currently logged in) and then displays the relevant information based on that user. But unfortunately no results are displayed.

With the following code:

$register="SELECT * from register";

All of the details are shown from every user, which is correct because that is the query. I have the following code:

<?php
require "dbconn.php";

$reg="SELECT * from students WHERE username = '".   $_SESSION['username']."'";

$res = $connect->query($reg);

$numrow = $res->num_rows; 

$count = 0;
while ($count < $numrow) 
{
$row = $res->fetch_assoc();
extract($row);    

$count = $count + 1;
}         
?>

<h2>Sign the Register</h2>
<form action="sign.php" method="post">
<div><textarea name="username" rows="3" cols="60" readonly="true"  required="true"> <?php echo $_SESSION['username'];?></textarea></div>
<div><textarea name="firstName" rows="3" cols="60" readonly="true"  required="true"><?php echo $firstName;?></textarea></div>
<div><textarea name="surname"  rows="3" cols="60" readonly="true"  required="true"><?php echo $surname;?></textarea></div>
<div><textarea name="course"  rows="3" cols="60" readonly="true" required="true"><?php echo $course;?></textarea></div>
<div><textarea name="subject" rows="3" cols="60" placeholder="Please Enter  Your Subject..." required="true"></textarea></div>
<div><textarea name="level" rows="3" cols="60" readonly="true required="true"         
<?php echo $level;?></textarea></div>
<div><textarea name="date" rows="3" cols="60" readonly="true" required="true">
<?php date_default_timezone_set("Europe/London"); echo date('Y-d-m');?      
</textarea></div><div><textarea name="time" rows="3" cols="60" readonly="true" required="true"> 
<?php date_default_timezone_set("Europe/London"); echo date('H:i:s A');?
</textarea></div>
<div><input type="submit" value="Sign Register"></div>
</form>

And sign.php is:

{
   if(array_key_exists('username', $_POST) && array_key_exists('firstName',   $_POST) && array_key_exists('surname', $_POST) && array_key_exists('course',  $_POST) && array_key_exists('subject', $_POST) && array_key_exists('level',  $_POST) && array_key_exists('date', $_POST) && array_key_exists('time', $_POST))
    $stmt = $db->prepare('INSERT INTO register (username, firstName, surname,  course, subject, level, date, time) VALUES (:username, :firstName, :surname,  :course, :subject, :level, :date, :time)');
     $stmt->execute(array(':username' => htmlspecialchars($_POST['username']),
     ':firstName' => htmlspecialchars($_POST['firstName']),
     ':surname' => htmlspecialchars($_POST['surname']),
     ':course' => htmlspecialchars($_POST['course']),
     ':subject' => htmlspecialchars($_POST['subject']),
     ':level' => htmlspecialchars($_POST['level']),
     ':date' => htmlspecialchars($_POST['date']),
     ':time' => htmlspecialchars($_POST['time'])));
      $affected_rows = $stmt->rowCount(); 
       }

So all of the sign and register page work fine. All of the correct data is sent and can be shown in the database. But when I specify the user to be shown, via the WHERE clause shown above, no results are shown.

But there is a user with the username of i001. If I manually type in the following query using the command line:

INSERT INTO register (username, firstName, surname, course, subject,   level, date, time) VALUES ('i001','Tom', 'Sm', 'Sociology',   'IntroductionToSociology', 'I','11.05.2015','15.05');

Then that result is shown when the user i001 is logged in, so that works fine. But using the register and sign.php form then it will not work. But regarding the register, the following code:

<?php echo $_SESSION['username'];?>

Does display the correct user logged in. And all of the data typed in the form does save correctly to the database. But will not be shown with the WHERE session username clause is present.

So basically any ideas how to solve this?

EDIT: It has something to do with the form/query. If I type in the query manually using MySQL Client it works perfectly. But when I type in using the form, it is sent to the database but not in the correct order. So not getting the correct username etc

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥170 如图所示配置eNSP
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改
    • ¥20 wireshark抓不到vlan
    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
    • ¥15 stata安慰剂检验作图但是真实值不出现在图上
    • ¥15 c程序不知道为什么得不到结果
    • ¥15 键盘指令混乱情况下的启动盘系统重装