将用户名传递给php中的另一个页面

时间:2013-11-18 19:01:07

标签: php

管理员将访问他的页面Admin.php他将看到所有用户旁边有2个按钮(编辑和删除),编辑按钮将管理员带到用户的个人资料,以使用Home2.php修改他的设置。问题是我无法正确传递用户名,当管理员修改设置时,数据库中没有更新。

admin.php的

<html>
<head>

<title>Untitled Document</title>

<style type="text/css">
@import url("style2.css");

</style>
</head>

<body>
<div align="center"><?php
$con=mysqli_connect("localhost","root","123","data1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM users");

echo "<table align='center' id='rounded-corner'>";
echo "<thead>
        <tr>
        <th scope='col' class='rounded-company'>ID</th>
        <th scope='col' class='rounded-company'>Username</th>
        <th scope='col' class='rounded-q1'>Password</th>
        <th scope='col' class='rounded-q2'>Filter Status</th>
        <th scope='col' class='rounded-q3'>Led Status</th>
        <th scope='col' class='rounded-q4'>Heater Status</th>
        <th scope='col' class='rounded-q4'>Edit</th>
        <th scope='col' class='rounded-q4'>Delete</th>
        </tr>
    </thead>";
    echo "<tfoot>";
    echo "</tfoot>
    <tbody>";

 while($row = mysqli_fetch_array($result))
  {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['password'] . "</td>";
  echo "<td>" . $row['filter_st'] . "</td>";
  echo "<td>" . $row['led_st'] . "</td>";
  echo "<td>" . $row['heat_st'] . "</td>";
   echo "<td>";
   echo "<form action='home2.php' method='post'>";
    echo "<input type='hidden' id='username' name='username' value='".$row['username']."'>";
    echo "<input name='edit' type='submit' id='edit' value='Edit' />";
echo "</form>";
echo "</td>";
  echo "<td>";
  echo "<form action='delete.php' method='post'>";
  echo "<input type='hidden' id='user_id' name='user_id' value='".$row['id']."'>";
  echo "<input name='delete' type='submit' id='delete' value='Delete' />";
echo "</form>";
echo "</td>";
     echo "</tr>";
   }
  echo "</tbody>
 </table>";

 mysqli_close($con);
 ?></div>
<br />
<br />

<br />
<br />



</body>
 </html>

Home2.php

 <?php
 include('conn.php');
session_start();
$_SESSION['username'] = $_POST['username'];
if(isset($_POST['usaername'])) { 
  }
if (!isset($_SESSION['username']))
    {
        header('Location: index.php');
    }
    exec('mode com5: baud=9600 data=8 stop=1 parity=n xon=no');

$switch1 = "";
$switch2 = "";
$switch3 = "";


if (isset($_GET['action'])) {

 $switch1 = $_GET['action'];
 $switch2 = $_GET['action'];
 $switch3 = $_GET['action'];


 switch ($switch1) {
 case "on1":
 $fp = fopen("com5", "w");
 fwrite($fp, chr(97));
  fclose($fp);
 break;

 case "off1":
 $fp = fopen("com5", "w");
 fwrite($fp, chr(98));
 fclose($fp);
 break;


 }
 switch ($switch2) {
 case "on2":
 $fp = fopen("com5", "w");
 fwrite($fp, chr(99));
 fclose($fp);
 break;

 case "off2":
 $fp = fopen("com5", "w");
  fwrite($fp, chr(100));
 fclose($fp);
 break;


 }
  switch ($switch3) {
 case "on3":
 $fp = fopen("com5", "w");
 fwrite($fp, chr(101));
 fclose($fp);
 break;

 case "off3":
 $fp = fopen("com5", "w");
 fwrite($fp, chr(102));
 fclose($fp);
  break;


}
}
?>
<?php
$host = "localhost";
$user = "root";
$pass = "123";
$db="data1";
mysql_connect($host,$user,$pass);
mysql_select_db($db);
$query = "SELECT filter_st,led_st,heat_st FROM users WHERE username='".$_SESSION['username']."'";
$result = mysql_query($query);
echo "<table border='1' align='center'>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "<br /><tr><td>Filter<td>" . $row['filter_st'],"</tr>","<tr><td>Led<td>" . $row['led_st'],"</tr>","<tr><td>Heater<td>" . $row['heat_st'],"</tr>";
}
echo "</table>"; //Close the table in HTML
mysql_close();
?>
<html>
<head>

<title>PHP Tutorials - Demos</title>
</head>
<body>
<h3>Welcome to Pets at Home, <u><?php echo $_SESSION['username']; ?></u></h3>
<center>

<div>
<font size="4">Filter Switch</font><br />
<table width="30">
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=on1" ?>">On</a></td><td><?php if($switch1 == 'on1'){
    echo "<b>ON</b>";
    $host = "localhost";
$user = "root";
$pass = "123";
$db="data1";
mysql_connect($host,$user,$pass);
mysql_select_db($db);
$query2 = "UPDATE users SET filter_st = 'ON' WHERE username = '".$_SESSION['username']."'";
$result2 = mysql_query($query2);
    }?></td></tr>
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=off1" ?>">Off</a></td><td><?php if($switch1 == 'off1'){
    echo "<b>OFF</b>";
$host = "localhost";
$user = "root";
$pass = "123";
$db="data1";
mysql_connect($host,$user,$pass);
mysql_select_db($db);
$query3 = "UPDATE users SET filter_st = 'OFF' WHERE username = '".$_SESSION['username']."'";
$result3 = mysql_query($query3);
    } ?></td></tr>
</table>
</div>
<div>
<font size="4">Led Switch</font><br />
<table width="30">
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=on2" ?>">On</a></td><td><?php if($switch2 == 'on2'){
    echo "<b>ON</b>";
    $host = "localhost";
$user = "root";
$pass = "123";
$db="data1";
mysql_connect($host,$user,$pass);
mysql_select_db($db);
$query3 = "UPDATE users SET led_st = 'ON' WHERE username = '".$_SESSION['username']."'";
$result3 = mysql_query($query3);
    } ?></td></tr>
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=off2" ?>">Off</a></td><td><?php if($switch2 == 'off2'){
    echo "<b>OFF</b>";
    $host = "localhost";
$user = "root";
$pass = "123";
$db="data1";
mysql_connect($host,$user,$pass);
mysql_select_db($db);
$query4 = "UPDATE users SET led_st = 'OFF' WHERE username = '".$_SESSION['username']."'";
$result4 = mysql_query($query4);
    } ?></td></tr>
</table>
</div>
<div>
<font size="4">Heater Switch</font><br />
<table width="30">
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=on3" ?>">On</a></td><td><?php if($switch3 == 'on3'){
    echo "<b>ON</b>";
    $host = "localhost";
$user = "root";
$pass = "123";
$db="data1";
mysql_connect($host,$user,$pass);
mysql_select_db($db);
$query5 = "UPDATE users SET heat_st = 'ON' WHERE username = '".$_SESSION['username']."'";
$result5 = mysql_query($query5);
    } ?></td></tr>
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=off3" ?>">Off</a></td><td><?php if($switch3 == 'off3'){
    echo "<b>OFF</b>";
    $host = "localhost";
$user = "root";
$pass = "123";
$db="data1";
mysql_connect($host,$user,$pass);
mysql_select_db($db);
$query6 = "UPDATE users SET heat_st = 'OFF' WHERE username = '".$_SESSION['username']."'";
$result6 = mysql_query($query6);
    } ?></td></tr>
</table>
</div>

</center>
<p align="right">This is logged in page - <a href="logout.php">Logout</a></p>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

session_start();    
if(isset($_POST['username'])) { 
   $_SESSION['username'] = $_POST['username'];
}

应该是username而不是 usaername ,而且您不是在调用session_start()

答案 1 :(得分:0)

您的问题是代码中没有session_start()活动,它永远不会启动会话并将值分配给会话。 在php文件的第一行,添加session_start()函数,然后重试。 在include()

之后执行session_start();