问题描述:

i am creating android application that need connection with php mysql the application work perfect in the android side.

the problem is in the php file when i am trying to store $_SESSION for id from file to file to file the php display an error :

Undefined variable id in c:\wamp\www\android_connect\status.php on line 53

how can i fix this error i will post the three file of php that contain

  • register
  • login
  • status

register.php

<?php

/*

* Following code will create a new product row

* All product details are read from HTTP Post Request

*/

// array for JSON response

$response = array();

if(empty($_POST['user']) || empty($_POST['password'])){

$response["success"] = 0;

$response["message"] = "enter Both Fields";

// echoing JSON response

die (json_encode($response));

}

// check for required fields

else if (isset($_POST['user']) && isset($_POST['password']) ) {

$user = $_POST['user'];

$password = $_POST['password'];

// include db connect class

require_once __DIR__ . '/db_connect.php';

// connecting to db

$db = new DB_CONNECT();

//check if the user exist !!!!

$check = mysql_query("SELECT * FROM users WHERE user = '$user'");

$count_query = mysql_num_rows($check);

if($count_query > 0){

$response["success"] = 0;

$response["message"] = "User Already Exist!!";

// echoing JSON response

die (json_encode($response));

}else if(strlen($user)<6){

$response["success"] = 0;

$response["message"] = "Your user Name Must bigger than 6 Char.";

// echoing JSON response

die (json_encode($response));

}else if(strlen($password)<8){

$response["success"] = 0;

$response["message"] = "Your Password Must bigger than 8 Char.";

// echoing JSON response

die (json_encode($response));

}else{

// mysql inserting a new row

$result = mysql_query("INSERT INTO users(user, password) VALUES('$user', '$password')");

//**************************

//added and can be deleted

//*************************

$id = mysql_insert_id();

// check if row inserted or not

if ($result) {

// successfully inserted into database

//**************************

//added and can be deleted

//*************************

$_SESSION['id'] = mysql_insert_id();

$response["success"] = 1;

$response["message"] = "User successfully created.";

// echoing JSON response

die (json_encode($response));

} else {

// failed to insert row

$response["success"] = 0;

$response["message"] = "Oops! An error occurred.";

// echoing JSON response

die (json_encode($response));

}

}

} else {

// required field is missing

$response["success"] = 0;

$response["message"] = "Required field(s) is missing";

// echoing JSON response

die (json_encode($response));

}

?>

login.php

<?php

ob_start();

@session_start();

//array for JSON response

$response = array();

// check for required fields

if(empty($_POST['user']) || empty($_POST['password'])){

$response["success"] = 0;

$response["message"] = "enter Both Fields";

// echoing JSON response

die (json_encode($response));

}

else if (isset($_POST['user']) && isset($_POST['password']) ) {

$user = $_POST['user'];

$password = $_POST['password'];

// include db connect class

require_once '/db_connect.php';

// connecting to db

$db = new DB_CONNECT();

// mysql inserting a new row

$sql = mysql_query("Select * from users where user='$user' and password = '$password'")or die(mysql_error());

$count_query = mysql_num_rows($sql);

if($count_query >0){

//*******************************

//added can be deleted

//*******************************

$user_details = mysql_fetch_array($sql);

$response["success"] = 1;

$id = $user_details['id'];

$_SESSION['id'] = $id;

$response['id'] = $user_details['id'];

$response["message"] = "correct Informations";

// echoing JSON response

echo json_encode($response);

}

else{

$response["success"] = 0;

$response["message"] = "Wrong User Or Pass";

// echoing JSON response

echo json_encode($response);

}

}

else {

// required field is missing

$response["success"] = 0;

$response["message"] = "Required field(s) is missing";

// echoing JSON response

echo json_encode($response);

}

ob_end_flush();

?>

status.php

 <?php ob_start();

//array for JSON response

$response = array();

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

}

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(); ?>

i know that the error is in the insert query but i do not know why it do not assign the sesion id to the variable $id.

网友答案:

You need to start the Session in status.php first, I'm pretty sure in register.php you need to do the same thing.

相关阅读:
Top