未定义的索引:userID错误

时间:2014-02-03 19:03:23

标签: php login userid

登录后,我将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);
 ?>

1 个答案:

答案 0 :(得分:1)

在每个文件的顶部添加:

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

此外,当你这样做时:

$userID = $_POST['userID'];

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

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