如何解决错误:
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/