-1

how to fix the error: Undefined variable : id in c:\wamp\www\android_connect\status.php in 38 what i am doing wrong ???

status.php

<?php
ob_start();

session_start();
//array for JSON response
$response = array();

if(isset($_SESSION['id']))
{

   $id= $_SESSION['id'];


} 

//var_dump ($id);

// include db connect class
    require_once '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

// check for required fields
if(empty($_POST['status'])){
       $response["success"] = 0;
       $response["message"] = "Your Text Field is empty";
        // echoing JSON response
        die (json_encode($response));


}
else if (isset($_POST['status'])) {

    $status = $_POST['status'];


    $sql = mysql_query("INSERT INTO status (status_text, uid)VALUES('$status', '$id')")or die(mysql_error());


      if ($sql) {
        // successfully inserted into database

        $response["success"] = 1;
        $response["message"] = "Status Saved.";

        // echoing JSON response
        die (json_encode($response));
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Error in saving the status.";

        // echoing JSON response
        die (json_encode($response));
    }

  }

ob_end_flush();
?>

the error is in the insert query but i do not know how to fix it. it have something with the $_SESSION can anyone help me ??

user3499032
  • 25
  • 1
  • 11
  • Well, `$id` is only defined `if(isset($_SESSION['id']))`. Otherwise it does not exist. – deceze Apr 09 '14 at 11:36
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – ChrisGPT was on strike Apr 09 '14 at 12:09

2 Answers2

1

The error you get means $id is unset. In your code there isn't a check for the $id variable.

Change else if (isset($_POST['status'])) { to else if (isset($_POST['status']) && isset($id)) {

and add an else statement, something like this:

else {
   // failed to insert row because of missing $id
    $response["success"] = 0;
    $response["message"] = "Error in saving the status, session variable $id missing";

    // echoing JSON response
    die (json_encode($response));
}
vanMartijn
  • 172
  • 5
0

Change the insert query like this,

$sql = mysql_query("INSERT INTO status (status_text, uid)VALUES('".$status."', '".$id."')")or die(mysql_error());