Fetching the data from sql join using PHP - php

I am trying to get the data from my join query.
But after running it. It says that undefined index. Please explain to me what's wrong like I'm a potato. Thanks :)
$join_query = "select customer.customer_email, item.ItemId, item.ItemPrice, cart.qty
from customer, item, cart
where customer.customer_email = '" . $_SESSION['customer_email'] . "' AND item.ItemId = cart.orderId";
$run_join = mysqli_query($con, $join_query);
while($row_boat = mysqli_fetch_array($run_join)){
$sales_email = $_SESSION['customer_email'];
$sales_itemid = $row_boat['item.ItemId'];
$sales_itemprice = $row_boat['item.ItemPrice'];
$sales_qty = $row_boat['cart.qty'];
$sales_total = $sales_itemprice * $sales_qty;
//$sales_date =
$insert_sales = "insert into sales (customer_email, ItemId, ItemPrice, Quantity, Total)
values ('$sales_email', $'sales_itemid', '$sales_itemprice', '$sales_qty', '$sales_total')";
$run_sales = mysqli_query($con, $insert_sales);

while($row_boat = mysqli_fetch_array($run_join, MYSQLI_ASSOC))
This should fix your error

Related

Update MySQL data based on radio button

I am facing a challenge right now.
I have a return page, if I selected the first row via the radio button, I would like to change the history_status inside my order history table from "DONE" to "RETURN PENDING". The reason why I am doing this is to run a query to show only history_status value with "DONE".
Below are the code I am working with:
<?php
$db = mysqli_connect('localhost','root','','customercaremodule');
date_default_timezone_set('Asia/Kuala_Lumpur');
$FBdate = date("Y-m-d H:i:s");
$sql = "SELECT p.product_id ,p.product_name , p.price, p.product_description, o.history_id ,o.qty , o.subtotal, o.history_datetime , o.history_status FROM product p, orderhistory o where o.product_id = p.product_id AND o.history_status = 'DONE'";
$result=mysqli_query($db,$sql);
$Cntsql = "SELECT count(return_id) AS total FROM retrn";
$res = mysqli_query($db,$Cntsql);
$value = mysqli_fetch_assoc($res);
$num = $value['total'];
if (isset($_POST['submitbuttonform']))
{
$return_id = $num+1;
$return_status = 'PENDING';
$return_reason = $_POST['reasonselected'];
$return_option = $_POST['returnoption'];
$return_datetime = $FBdate;
$history_id = $_POST['selectitemradio'];
$history_status = "UPDATE orderhistory SET history_status = 'RETURN PENDING'";
$sql = "INSERT INTO `retrn`(`return_id`, `return_status`, `return_reason`, `return_option`, `return_datetime`, `history_id`) VALUES ('$return_id','$return_status','$return_reason','$return_option','$return_datetime','$history_id')";
$sql = "INSERT INTO 'orderhistory'('history_status') VALUES ('$history_status')";
$result=mysqli_query($db,$sql);
Order History Table
Return page layout
From your code
$history_id = $_POST['selectitemradio']; I assume you get the history id from the page.
So you can use the history_id in the where clause. Now only the selected row will be updated.
UPDATE orderhistory SET history_status = 'RETURN PENDING' where history_id=$history_id

PHP / mysql join

I need to get 3 tables's values , from first I need to get aff_id where v_id = 5 , from second I need to get user id where aff_id = first's aff_id , and from third I need to get username , email , id where id = second's aff_id . I can't write correct mysql query to get data , please , help me to get it . Here is my wrong code
SELECT * FROM wp_vendor_affiliates WHERE vendor_id = 21 LEFT JOIN wp_affiliate_wp_affiliates
SELECT wp_vendor_affiliates.affiliate_id ,
wp_affiliate_wp_affiliates.user_id
FROM wp_vendor_affiliates
INNER JOIN wp_affiliate_wp_affiliates INNER JOIN
Please , hetp me , and correct my query . Thanks fot helping and for support
Use this code
$id1 = 5;
$query1 = "SELECT * FROM table1 WHERE v_id='{$id1}'";
$result1 = mysqli_query($con,$query1);
$row1 = mysqli_fetch_array($result1);
$id2 = $row1['aff_id'];
$query2 = "SELECT * FROM table2 WHERE aff_id='{$id2}'";
$result2 = mysqli_query($con,$query2);
$row2 = mysqli_fetch_array($result2);
$id3 = $row2['aff_id'];
$query3 = "SELECT * FROM table3 WHERE id='{$id3}'";
$result3 = mysqli_query($con,$query3);
$row3 = mysqli_fetch_array($result3);
$id2 = $row3['aff_id'];
Here $con is the connection to your database
$con= mysqli_connect("localhost","root","","data_base");
I used here connection to the localhost. which you'd have to change most certainly. It's a bit long but basic

PHP stops at, and shows, a single line of code

Recently I copied a PHP script to use it for another database. After I correctly edited all the words and links, I got an weird error.
SELECT naam, aantal, prijs
FROM boeken, bestelling
WHERE boeken.Boekcode = bestelling.Boekcode
AND bestelling.Boekcode IN ('101','102')
AND bestelnummer = 3;
It's not a regular error that say something like
Error on line 42
Basically what the code does, is that when you order books and filled in a form (Name, last name, email etc) it puts that in a database. And afterwards puts it in a "thankyou.html" page.
Here's part of the code that causes this
mysqli_query($con, $query) or die($query . "<br>");
$bestelnummer = MYSQLI_INSERT_ID($con);
$object = array_filter($object);
$objectnaam = join("','",array_keys($object));
$object = http_build_query($object);
$object = str_replace('=', ',', $object);
$object = str_replace('&', "),($bestelnummer,", $object);
$query = "INSERT INTO bestelling (bestelnummer, Boekcode, aantal) VALUES ($bestelnummer,$object)";
$result = mysqli_query($con, $query) or die($query."<br>");
$aantal = "SELECT naam, aantal, prijs FROM boeken, bestelling WHERE boeken.Boekcode = bestelling.Boekcode AND bestelling.Boekcode IN ('".$objectnaam."') AND bestelnummer = $bestelnummer";
$result = mysqli_query($con, $aantal) or die($aantal . "<br>");
$res = mysqli_fetch_all($result);
$prijs = 0;
The $object is an array of the books you choose.
I've Google'd for this problem, and yes, I have Apache and everything enabled.
Sorry if I'm unclear, it's been a while, I can answer any questions you might have.
Well you echo out the query if it fails.
$aantal = "SELECT naam, aantal, prijs FROM boeken, bestelling WHERE boeken.Boekcode = bestelling.Boekcode AND bestelling.Boekcode IN ('".$objectnaam."') AND bestelnummer = $bestelnummer";
$result = mysqli_query($con, $aantal) or die($aantal . "<br>");
You might want to check the error message that is returned from the database using mysqli_error().
$result = mysqli_query($con, $aantal) or die(mysqli_error() . "<br>");
first need to refine
$query = "INSERT INTO bestelling (bestelnummer, Boekcode, aantal) VALUES ($bestelnummer,$object)";
you are selecting values insertion for 3 columns and passing only two values
For second query try to use backticks for tablename and column

Query error not getting numbers

Here is my query, I am trying to get numbers from another table using a number from another table here is my query ...
$order_id = $template_vars['{order_name}'];
// Query to find the product id for the current order and then set it to a variable
$query="SELECT product_id FROM ps_order_detail WHERE id_order = $order_id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$Product_id = $row['0'];
// get all the custom part numbers and set them to the variables
$customnumbers ="SELECT API, SWAIM, JOHN_CRANE, SNOW_WELL, MIDAS, QUINN, WILSON, WEATHERFORD, HF, BLACK_GOLD, EDI, SO_CAL_PUMPS, WEST_RIVER
FROM ps_product_part_number WHERE Product_ID = $Product_id";
$secondresult = mysql_query($customnumbers);
$secondrow = mysql_fetch_row($secondresult);
$API = $secondrow['0'];
$SWAIM = $secondrow['1'];
$JOHN_CRANE = $secondrow['2'];
$SNOW_WELL = $secondrow['3'];
$MIDAS = $secondrow['4'];
$QUINN = $secondrow['5'];
$WILSON = $secondrow['6'];
$WEATHERFORD = $secondrow['7'];
$HF = $secondrow['8'];
$BLACK_GOLD = $secondrow['9'];
$EDI = $secondrow['10'];
$SO_CAL_PUMPS = $secondrow['11'];
$WEST_RIVER = $secondrow['12'];
How about doing it in one step with a join?
SELECT ppn.*
FROM ps_product_part_number ppn
join ps_order_detail od on od.product_id = ppn.Product_ID
WHERE od.id_order = $order_id

PHP, MySQL select all if

I have problem with PHP and MySQL please help..
$lokalita_s = $_POST['lokalita_s'];
$query = "SELECT nazov, lokalita FROM reality WHERE lokalita = '".$lokalita_s."' ORDER BY id";
............
But if ($lokalita_s == "nezáleží")... then i want to select every thing from database..
something like this :
$query = "SELECT nazov, lokalita FROM reality ORDER BY id";
............
This is not working :
$lokalita_s = 0;
$lokalita_s = NULL;
$lokalita_s = *;
I really dont want to use it like if else.. because i want to use more variables in that query and it won't be effective
Try
$lokalita_s = $_POST['lokalita_s'];
$wherClause = null;
if($lokalita_s != "nezáleží") {
$wherClause = "WHERE lokalita = '" . $lokalita_s . "'";
}
$query = "SELECT nazov, lokalita FROM reality $wherClause ORDER BY id";
Something along these lines? (This works in Oracle)
$lokalita_s = $_POST['lokalita_s'];
$query = "
SELECT nazov, lokalita FROM reality WHERE lokalita = '". $lokalita_s."'
UNION
select nazov,lokalita from reality where '". $lokalita_s. "' = 'nezáleží'
order by id
"

Categories