php - php mysql如何解决此错误:Undefined variable :id in C:\

标签 php mysql session error-handling

如何解决错误:
undefined variable :38 中c:\ wamp \ www \ android_connect \ status.php中的ID
我在做什么错???

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

该错误是在插入查询,但我不知道如何解决它。
$ _SESSION有东西可以帮助我吗?

最佳答案

您得到的错误意味着未设置$ id。在您的代码中,没有检查$ id变量。

else if (isset($_POST['status'])) {更改为else if (isset($_POST['status']) && isset($id)) {
并添加一个else语句,如下所示:

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));
}

关于php - php mysql如何解决此错误:Undefined variable :id in C:\,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22961139/

相关文章:

PHP Mcrypt,它到底有多安全?

php - 从 mysql 检索一条记录并将其存储在 PHP 的数组中

php - 将字符串转换为数字并返回字符串?

php - 使用 PHP DOM 在 html 标记的开头插入创建的元​​素

php - 使用 mysqli 连接 localhost 或 127.0.0.1 时出现 HY000/2003 错误

php - 数组作为 session 变量

php - 我如何在服务器上搜索 php session ?

mysql 2 多个 WHERE 子句的连接错误

mysql - 创建优化的mysql数据库

session - 使用 sessionman.vim 插件恢复上次打开的 Vim session