0

我遇到了一个问题,例如:我们有 2 个用户,所以用户 A 将数据插入数据库,并且可以毫无问题地检索它。用户 B 将数据插入数据库,当用户 B 想要检索数据时,用户 B 获取的是用户 A 而不是用户 B 的数据。

什么会导致这个问题?

这是我从 mysql 数据库获取数据的 php 代码:

<?php

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

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

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

// check for post data
if (isset($_GET["subject_id"])) {
    $subject_id = $_GET['subject_id'];

    // get a product from products table
    $result = mysql_query("SELECT * FROM subject_offered WHERE subject_id = $subject_id");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $product = array();
            $product["subject_id"] = $result["subject_id"];
            $product["lecturer_name"] = $result["lecturer_name"];
            $product["time_offered"] = $result["time_offered"];
            $product["subject_details"] = $result["subject_details"];

            //$product["updated_at"] = $result["updated_at"];
            // success
            $response["success"] = 1;

            // user node
            $response["product"] = array();

            array_push($response["product"], $product);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No subject found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No subject found";

        // echo no users JSON
        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);
}
?>
4

1 回答 1

0
 // get a product from products table
    $result = mysql_query("SELECT * FROM subject_offered WHERE subject_id = $subject_id");

应该

  // get a product from products table
       $result = mysql_query("SELECT * FROM subject_offered WHERE subject_id = '$subject_id' ");
//some times when you miss '' this single commas then it also create a problem
于 2012-12-15T16:43:57.253 回答