无法使用会话变量更新表

时间:2016-07-12 20:43:26

标签: php mysql session variables

我想使用会话变量更新特定用户的信息,但是当我在where子句中使用会话变量时,该行不受影响。

这里我存储会话变量的值'用户名'进入变量$ user,我在update where子句中使用$ user变量来更新行。但是当我更新它没有得到更新。 这是我使用的代码:

<?php
//session_start();
 include '../global_config/databaseconfig.php';
global $user;
if(isset($_SESSION['username'])){
    $user = $_SESSION['username'];}
echo $user;
 global $fname;global $lname;global $phno;global $gender;
if(isset($_POST['fname'])){ $fname = $_POST['fname']; }
if(isset($_POST['lname'])){ $lname = $_POST['lname']; }
if(isset($_POST['phno'])){ $phno = $_POST['phno']; }
if(isset($_POST['gender'])){ $gender = $_POST['gender']; }
echo $fname;
// $lname = $_POST['lname'];
//$user variable contains the username

  $tbl_name = "members";
  // $host = "localhost"; // Host name
  // $username = "root"; // Mysql username
  // $password = ""; // Mysql password
  // $db_name = "login"; // Database name
  // $tbl_name = "members"; // Table name
  //CONNECTION
  $mysqli = new mysqli($host,$username,$password,$db_name);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

// echo $mysqli->host_info . "\n";

$mysqli = new mysqli("127.0.0.1",$username,$password,$db_name, 3306);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
// echo $mysqli->host_info . "\n";
$result = $mysqli->query("UPDATE `members` SET `firstname`='$fname' WHERE `username`='$user'");
printf("Affected rows (SELECT): %d\n", $mysqli->affected_rows);


 ?>

1 个答案:

答案 0 :(得分:0)

感谢您的帮助。

   //use this at the beginning  of the file
    @session_start(); 

需要在使用之前启动会话。