gpt4 book ai didi

php - PH值错误: notice : undefined variable : id in c:\wamp how to fix it?? $_SESSION 错误

转载 作者:行者123 更新时间:2023-11-30 00:25:00 27 4
gpt4 key购买 nike

我正在创建需要与 php mysql 连接的 Android 应用程序,该应用程序在 Android 端完美运行。

问题出在 php 文件中,当我尝试将 id 的 $_SESSION 从一个文件存储到另一个文件时,php 显示错误: 第 53 行 c:\wamp\www\android_connect\status.php 中 undefined variable id

我该如何修复这个错误,我将发布包含以下内容的三个 php 文件

  • 注册
  • 登录
  • 状态

注册.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));
}
?>

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

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

我知道错误出现在插入查询中,但我不知道为什么它不将 session ID 分配给变量 $id。

最佳答案

您需要首先在 status.php 中启动 session ,我很确定您需要在 register.php 中执行相同的操作。

关于php - PH值错误: notice : undefined variable : id in c:\wamp how to fix it?? $_SESSION 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22953755/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com