I am working on an Android project and I am completely new to Android. I'm following the below tutorial link. My project is about police registry system. I am connecting to MySQL database with the help of PHP and JSON.
I have created a form where user's can register and they can login using their user name and password. Also I have made a form where user can register their complaint and complaints are stored in a single database known as db_complaints
in the database. I am able to retrieve all complaints using following PHP code.
<?php
/*
Our "config.inc.php" file connects to database every time we include or require
it within a php script. Since we want this script to add a new user to our db,
we will be talking with our database, and therefore,
let's require the connection to happen:
*/
require("config.inc.php");
//initial query
if (!empty($_POST)) {
//initial query
$query = "INSERT INTO complaints ( username, title, message ) VALUES ( :user, :title, :message ) ";
//Update query
$query_params = array(
':user' => $_POST['username'],
':title' => $_POST['title'],
':message' => $_POST['message']
);
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["post_id"] = $row["post_id"];
$post["username"] = $row["username"];
$post["title"] = $row["title"];
$post["message"] = $row["message"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}}
?>
i want to retrieve a particular user complaints separately so they can view there complaints in my_complaints form but i am not able to do this and i have been stuck.
Your code looks pretty neat using prepared statements and error handling, so where is your problem actually?
$query = "SELECT * FROM complaints WHERE username = :user";
$query_params = array(':user' => $_POST['username']);
That query should do the trick to retrieve complaints from a specific user.