Hello Guys. I am working on a android project. I am beginner in Android app, php and mysql. I am getting this error: "Undefined property: stdClass::$newsId"
How can I fix this error?
I am getting error in this line:
$newsId = $request -> {'newsId'};
The whole code:
<?php
include 'config/db_config.php';
$data = file_get_contents("php://input");
$request = json_decode($data);
$response = array();
$isValidRequest = false;
if(isset($request->{'action'})){
if($request->{'action'} == 'REGISTER_USER'){
$isValidRequest = true;
$userName = $request -> {'userName'};
$query = $connection->prepare("INSERT INTO user(name) VALUES (?)");
$query->bind_param("s", $userName);
$result = $query->execute();
if($result){
$response['userId'] = mysqli_insert_id($connection);
$response['status'] = true;
$response['responseCode'] = 0;
$response['message'] = "User Registered Successfully";
}else{
$response['status'] = false;
$response['responseCode'] = 102;
$response['message'] = "User Registered Failed";
}
}
if($request->{'action'} == 'ADD_NEWS'){
$isValidRequest = true;
$headline = $request -> {'headline'};
$description = $request -> {'description'};
$userId = $request -> {'userId'};
$query = $connection->prepare("INSERT INTO news(headline,description,user_id) VALUES (?,?,?)");
$query->bind_param("sss", $headline, $description, $userId);
$result = $query->execute();
if($result){
$response['newsId'] = mysqli_insert_id($connection);
$response['status'] = true;
$response['responseCode'] = 0;
$response['message'] = "News Added Successfully";
}else{
$response['status'] = false;
$response['responseCode'] = 103;
$response['message'] = "News Not Added";
}
}
if($request->{'action'} == 'GET_NEWS'){
$isValidRequest = true;
$userId = $request -> {'userId'};
$query = "SELECT n.id as newsId, u.id as userId, n.date_time as newsDateTime, u.date_time as userDateTime, n.*, u.* FROM news n INNER JOIN user u on n.user_id = u.id";
$result = mysqli_query($connection, $query);
if($result && mysqli_num_rows($result)>0){
$myNews = array();
$allNews = array();
while(($row = mysqli_fetch_assoc($result))!=null){
$news = array();
$news["newsId"] = $row['newsId'];
$news["newsBy"] = $row['name'];
$news["headline"] = $row['headline'];
$news["description"] = $row['description'];
$news["dateTime"] = $row['newsDateTime'];
$allNews[] = $news;
if($row['userId'] == $userId){
$myNews[] = $news;
}
}
$response['status'] = true;
$response['responseCode'] = 0;
$response['message'] = "News is Available";
$response['allNews'] = $allNews;
$response['myNews'] = $myNews;
}else{
$response['status'] = false;
$response['responseCode'] = 104;
$response['message'] = "News Not Available";
}
}
if($request->{'action'} == 'UPDATE_NEWS'){
$isValidRequest = true;
$userId = $request -> {'userId'};
$headline = $request -> {'headline'};
$description = $request -> {'description'};
$newsId = $request -> {'newsId'};
$query = "UPDATE news SET headline = '".$headline."',description = '".$description."' WHERE id = '".$newsId."'";
$result = mysqli_query($connection, $query);
if($result){
$response['newsId'] = $newsId;
$response['status'] = true;
$response['responseCode'] = 0;
$response['message'] = "News Updated Successfully";
}else{
$response['status'] = false;
$response['responseCode'] = 105;
$response['message'] = "News Not Updated";
}
}
if($request->{'action'} == 'DELETE_NEWS'){
$isValidRequest = true;
$userId = $request -> {'userId'};
$newsId = $request -> {'newsId'};
$query = "DELETE FROM news WHERE id = '".$newsId."'";
$result = mysqli_query($connection, $query);
if($result){
$response['newsId'] = $newsId;
$response['status'] = true;
$response['responseCode'] = 0;
$response['message'] = "News Deleted Successfully";
}else{
$response['status'] = false;
$response['responseCode'] = 106;
$response['message'] = "News Not Deleted";
}
}
if(!$isValidRequest){
$response['status'] = false;
$response['responseCode'] = 100;
$response['message'] = "Invalid request action";
}
}else{
$response['status'] = false;
$response['responseCode'] = 100;
$response['message'] = "Request action not defined";
}
echo json_encode($response);
?>
What I have tried:
honestly, i had not tried anything because I don't have any idea about the error. I visited many answers on stackoverflow and code project also website to seek answer. but i didn't understand it.