我正在创建需要与php-mysql连接的android应用程序,该应用程序在android端非常完美。
问题出现在php文件中,当我尝试从一个文件到另一个文件存储$_SESSION for id时,php显示错误:第53行c:\wamp\www\android_connect\status.php中未定义的变量id
我如何修复这个错误我将发布包含的php的三个文件
- 寄存器
- 登录
- 状态
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(); ?>
我知道错误在insert查询中,但我不知道它为什么不将sesion id分配给变量$id。
您需要首先在status.php中启动会话,我很确定在register.php中也需要这样做。