Does anyone know of a way that I can gat data from 2 tables in a MySql DB using PHP? My android app allows users to place items in a shopping cart(table in MySql). When the user clicks on the shopping cart button on in the app it loads the "Shopping Cart Activity" and loads a listview with everything they have added to their cart based on a "Session ID" or "Customer ID" if they are logged in. What I do right now is when the item is added to the cart I capture the Product ID, Product Name, and Image URL, Quantity, and Price. It works great and quickly. What I am wanting to do is since the Product Name, image url, and price are already in the "Products Table" I want to do the same thing but when the user adds an item to their cart only capture the Product ID and session ID. When the customer goes to the Shopping Cart Activity I want my PHP Script to grab the Product ID in the Shopping_Cart table then Search the Products Tables for that Product ID and return the Price, Image URL, Name, etc.
Here is my PHP Script:
if($The_Function=="LOAD_CART"){
$response = array();
require_once __DIR__ . '/db_connect.php';
$con = new DB_CONNECT();
$user_session = $_GET['session'];
$user_item_status = $_GET['status'];
$order_total = 0.00;
$result = mysql_query("SELECT * FROM shopping_cart WHERE SessionID LIKE '$user_session' AND Status LIKE '$user_item_status'");
if(!empty($result)){
if (mysql_num_rows($result) > 0) {
$response["cart_contents"] = array();
//$result = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
$myItems = array();
$myItems["product_id"] = $row["PID"];
$myItems["product_name"] = $row["Item"];
$myItems["product_price"] = $row["Price"];
$myItems["product_qty"] = $row["Qty"];
$myItems["image_url"] = $row["URL"];
$myItems["line_id"] = $row["ItemCount"];
$line_total = $row["Price"] * $row["Qty"];
$order_total = $order_total + $line_total;
array_push($response["cart_contents"], $myItems);
}
// success
$response["success"] = 1;
$response["order_total"] = $order_total;
// user node
// $response["products"] = array();
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
}
Here is what I am trying to do. This code does not work.
if($The_Function=="LOAD_CART2"){
$response = array();
require_once __DIR__ . '/db_connect.php';
$con = new DB_CONNECT();
$user_session = $_GET['session'];
$user_item_status = $_GET['status'];
$order_total = 0.00;
$result = mysql_query("SELECT * FROM shopping_cart WHERE SessionID LIKE '$user_session' AND Status LIKE '$user_item_status'");
if(!empty($result)){
if (mysql_num_rows($result) > 0) {
$response["cart_contents"] = array();
//$result = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
$line_total = $row["Price"] * $row["Qty"];
$line_ID = $row["ItemCount"];
$Quantity = $row["Qty"];
$Prod_ID = $row["PID"];
$result2 = mysql_query("SELECT * FROM products WHERE pid LIKE '$Prod_ID'");
$myItems = array();
$theItem=$row["name"];
$thePrice=$row["price"];
$theImage=$row["prod_image"];
$myItems["line_id"] = $line_ID;
$myItems["product_qty"] = $Quantity;
$myItems["product_id"] = $Prod_ID;
$myItems["product_name"] = $theItem;
$myItems["product_price"] = $thePrice;
$myItems["image_url"] = $theImage;
$order_total = $order_total + $line_total;
array_push($response["cart_contents"], $myItems);
}
}
// success
$response["success"] = 1;
$response["order_total"] = $order_total;
// user node
// $response["products"] = array();
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
THIS IS WHAT I JUST TRIED:
if($The_Function=="LOAD_CART2"){
$response = array();
require_once __DIR__ . '/db_connect.php';
$con = new DB_CONNECT();
$user_session = $_GET['session'];
$user_item_status = $_GET['status'];
$order_total = 0.00;
$result = mysql_query("SELECT s.*, p.* FROM shopping_cart AS s LEFT JOIN products AS p ON p.pid = s.PID WHERE s.SessionID ='$user_session' AND s.Status = '$user_item_status'");
if(!empty($result)){
if (mysql_num_rows($result) > 0) {
$response["cart_contents"] = array();
//$result = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
$myItems = array();
//FROM shopping_cart Table
$line_total = $row["Price"] * $row["Qty"];
$myItems["line_id"] = $row["ItemCount"];
$myItems["product_qty"] = $row["Qty"];
$myItems["product_id"] = $row["PID"];
//FROM product TABLE
$myItems["product_name"]=$row["p.name"];
$myItems["product_price"]=$row["p.price"];
$myItems["image_url"]=$row["p.prod_image"];
}
$order_total = $order_total + $line_total;
array_push($response["cart_contents"], $myItems);
}
// success
$response["success"] = 1;
$response["order_total"] = $order_total;
// user node
// $response["products"] = array();
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
My Scheme:
products TABLE
pid|name|price|created_at|prod_image|description|catagory
shopping_cart TABLE
FirstName|LastName|OrderNumber|CustomerID|Email|Price|Qty|Status|URL|PID|SessionID|CustomerType|ItemCount
You should use a LEFT JOIN instead of doing a separate query for each item.
Something like:
"SELECT s.*, p.* FROM shopping_cart AS s
LEFT JOIN products AS p ON p.pid = s.PID
WHERE s.SessionID ='$user_session' AND s.Status = '$user_item_status'
Assuming lowercase pid is in the products table, and capital PID is in the shopping_card table.
Related
I am unable to fetch last order_id from sql table and i have to increment that order id.
<?php
if (isset($_POST['items']) && isset($_POST['quantity']) &&
isset($_POST['room_no']) ) {
echo "hiii ----";
$items = $_POST['items'];
$quantity = $_POST['quantity'];
$room_no = $_POST['room_no'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
$order_id = mysql_query("SELECT order_id FROM orderitems ORDER BY date_time DESC LIMIT 1");
$order_id = $order_id + 1;
echo "items are ----" . $items;
$JSON_Received = $_POST["items"];
$obj = json_decode($JSON_Received, true);
foreach ($obj as $item) {
$name = $item['name'];
$count = $item['count'];
$result = mysql_query("INSERT INTO
orderitems(order_id,items,quantity,room_no)
VALUES('$order_id','$name','$count', '$room_no')");
$order_id = mysql_query("SELECT order_id FROM orderitems ORDER BY date_time
DESC LIMIT 1");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
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);
}
?>
I am unable to fetch last order_id from sql table and I have to increment that order_id after executing for loop.
I have searched many stackoverflow questions, But it didn't help
Help is much appreciated!!
SELECT MAX(order_id ) AS lastestOrder FROM orderitems ;
Why don't you use auto increment in the schema instead of doing it manually?
I have a problem with my PHP query. I just need to get the prescription for the clicked patient in listview.
Here is the table structure for prescription.
I need patient with id 79 to display all his corresponding prescription. Please help me.
Here is the PHP.
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
if (isset($_GET["uid"])) {
$uid = $_GET['uid'];
// get all products from products table
$result = mysql_query("select * from prescription") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["prescription"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$prescription = array();
$prescription["pres_id"] = $row["pres_id"];
$prescription["pres_name"] = $row["pres_name"];
//$product["price"] = $row["price"];
// push single product into final response array
array_push($response["prescription"], $prescription);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No prescriptions found";
// echo no users JSON
echo json_encode($response);
}
}else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
}
?>
To get all the prescriptions for a selected patient, wouldn't it be:
mysql_query("select * from prescription where patient_user_fkey = '$patientID'")or die(mysql_error());
instead of:
mysql_query("select * from prescription") or die(mysql_error());
I have this code passing a variable from a url. When i use $_GET method it returns me a json with no products found but when i give manually the the value that $user_email has from the url it returns me the correct json! what is wrong and how can i correct it? thank you
URL: http://***********/android_connect/get_all_products.php?user_email=m
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
$user_email= $_GET['user_email'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$test= "SELECT *FROM products WHERE user_email= '" .$user_email. "'";
//echo $test;
$result = mysql_query($test) or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["pid"] = $row["pid"];
$product["firstname"] = $row["firstname"];
$product["lastname"] = $row["lastname"];
$product["email"] = $row["email"];
$product["phone"] = $row["phone"];
$product["address"] = $row["address"];
$product["created_at"] = $row["created_at"];
$product["updated_at"] = $row["updated_at"];
$product["user_email"] = $row["user_email"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
Try this:
$test= "SELECT * FROM products WHERE user_email = '$user_email'";
EDIT:
$test= "SELECT * FROM products WHERE user_email = $user_email";
When I wrote the select statement it always return the last inserted row in the database. What is the problem, and how can I fix it?
Important NOTE: A friend of mine took the same code and it worked for her properly!
if (isset($_GET["name"])) {
$pid = $_GET['name'];
// get a product from products table
//)or die(mysql_error()
$result = mysql_query("SELECT * FROM food WHERE name = $pid");
//mysql_query($result,$con);
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["name"] = $result["name"];
$product["unit"] = $result["unit"];
$product["calory"] = $result["calory"];
$product["carbohydrate"] = $result["carbohydrate"];
$product["category"] = $result["category"];
// 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 item found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product 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);
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["name"] = $result["name"];
$product["unit"] = $result["unit"];
$product["calory"] = $result["calory"];
$product["carbohydrate"] = $result["carbohydrate"];
$product["category"] = $result["category"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
}
replace this with
while(mysql_num_rows($result) > 0 && ($result = mysql_fetch_array($result))) {
$product = array();
$product["name"] = $result["name"];
$product["unit"] = $result["unit"];
$product["calory"] = $result["calory"];
$product["carbohydrate"] = $result["carbohydrate"];
$product["category"] = $result["category"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
}
the result is array and you are not looping through it
so it givesonly one element in the array
This is because mysql_fetch_array is not in a loop, place it into the while loop and check.
Simply putting everything in a loop will not fix this. The code your gave will give the same result.. the last one.
$product needs to be declared BEFORE the loop otherwise, it will always be reset. Also, in order to populate the $product array without overwriting you will need to make it multidimensional
$product[]['name'] = $result["name"];
The ideal way of storing the products would be like this.. in my opinion.
$product = array();
while($result = mysql_fetch_array($result)) {
$product[$result['id']] = $result;
i'm having a problem where for instance: we have 2 users, so user A insert the data into the database and it can be retrieved with no problem. User B insert the data into the database and when user B want to retrieve the data, the user B gets the data of user A not user B.
What will be causing that problem?
Here my php code for getting data from mysql database:
<?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);
}
?>
// get a product from products table
$result = mysql_query("SELECT * FROM subject_offered WHERE subject_id = $subject_id");
should be
// 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