【问题标题】:Undefined index: userID error未定义索引:用户 ID 错误
【发布时间】:2014-02-03 19:03:22
【问题描述】:

登录后,我将 userID 存储在 SESSION 中。但是,当我调用 updateMarkerlocations.php 时,它说 userID 未定义。不知道我错过了什么。

login.php

session_start();
if (!isset($_POST['submit'])){

} else {
require_once("db_const.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
    exit();
}
 $username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT * from userinfo WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
    echo "<p>Invalid username/password combination</p>";
} else {
 $row = $result->fetch_assoc();

  setcookie("username", time() +60*60*24*30*365);
$_SESSION['userID'] = $row['userID']; 

     echo "<p>Logged in successfully!, Please close the window</p>";
}
}
?>       

updateMarkerLocations.php

 <?php
include 'db_const.php';


function insertMarkerLocations()
{
$markerCount = 0;
if (isset($_POST['markerCount']))
$markerCount = $_POST['markerCount'];

if(isset($_SESSION["userID"]))
{
 $userID = $_SESSION["userID"];
}

$con = mysql_connect(DB_HOST, DB_USER, DB_PASS);

mysql_select_db(DB_NAME);

$userID = $_POST['userID'];

for($i=0 ; $i < $markerCount; $i++){ 
      $index = $i;
      ++$index;
      $curMarkerID = $_POST["markerID$index"];
      $curLang = $_POST["lang$index"];
      $curLat = $_POST["lat$index"];
  // Now write the current marker details in to the db.
  $query = "INSERT INTO userinfo (userID, markerID, lang, lat ) VALUES ('$userID', '$curMarkerID', '$curLang', '$curLat')";
  mysql_query($query)
    or die(mysql_error());
}
$msg = "SUCCESS";
return $msg;
}

 $msg = insertMarkerLocations();
echo json_encode($msg);
 ?>

【问题讨论】:

标签: php login userid


【解决方案1】:

在每个文件的顶部添加:

if(!isset($_SESSION)) session_start();

另外,当你这样做时:

$userID = $_POST['userID'];

您应该确保$_POST['userID'] 存在:

if(isset($_POST['userID'])) $userID = $_POST['userID'];

【讨论】:

  • 分配时尝试回显$_SESSION['userID']
  • echo 适用于登录页面和 updatemarkerLocations 页面。所以这里有些东西坏了。我不明白为什么回显工作时找不到用户ID
  • 我已经更新了我的答案。如果不起作用,请告诉我错误行在哪里。
猜你喜欢
  • 2015-11-01
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2014-01-17
  • 2011-02-21
  • 2016-08-17
  • 2011-12-12
  • 1970-01-01
相关资源
最近更新 更多