PHP MySQL会话连接数据库

时间:2015-03-26 18:12:10

标签: php mysql sql database session

这是一个很长的问题,所以道歉。我有以下代码。      

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

 }
 ?>

这个想法是页面获取会话用户名(用户当前登录),然后根据该用户显示相关信息。但遗憾的是,没有显示任何结果。

使用以下代码:

$register="SELECT * from register";

所有细节都显示在每个用户身上,这是正确的,因为这是查询。我有以下代码:

<?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>

并且sign.php是:

{
   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(); 
       }

所以所有的签名和注册页面都可以正常工作。发送所有正确的数据,并可以在数据库中显示。但是当我通过上面显示的WHERE子句指定要显示的用户时,不会显示任何结果。

但是有一个用户名为i001。如果我使用命令行手动输入以下查询:

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

然后在用户i001登录时显示该结果,以便正常工作。但是使用register和sign.php表单则不起作用。但关于寄存器,以下代码:

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

显示正确的用户登录。并且表单中输入的所有数据都会正确保存到数据库中。但不会显示WHERE会话用户名子句存在。

基本上任何想法如何解决这个问题?

编辑:它与表单/查询有关。如果我使用MySQL客户端手动输入查询,它可以很好地工作。但是当我输入使用表单时,它会被发送到数据库但不是正确的顺序。所以没有得到正确的用户名等

0 个答案:

没有答案