Can Mysqli_query() calls be nested? - php

I am trying to implement the following code. What I want is to fetch the product id with column name prod_id from the table cart_details and then fetch the details for that product id with column name prod_id from the table products. But this code is not returning anything. Does this means that mysqli_query() calls cannot be nested?
<?php
$cart_id=$_POST['q'];
include "connection.php";
$cart_id=mysqli_real_escape_string($link,$cart_id);
$query="select product_id from cart_details where cart_id = $cart_id";
$result=mysqli_query($link,$query) or die(mysqli_error($link));
if($result)
{
while($row=mysqli_fetch_array($result))
{
$prod_id = $row['product_id'];
$prodDetail = "Select * from products where prod_id = $prod_id";
$prodResult = mysqli_query($link,$prodDetails) or die(mysqli_error($link));
if(!$prodResult){
echo "There was an error in fetching the product with product ID ".$prod_id;
}
else{
if(mysqli_num_rows($prodResult)==0)
{
echo "There is no item in this cart";
}
else{
while($prod=mysqli_fetch_array($prodResult)){
$prod_name=$prod['prod_name'];
$prod_price=$prod['prod_price'];
echo "<tr><td>".$prod_id."</td>";
echo "<td>".$prod_name."</td>";
echo "<td>".$prod_price."</td></tr>";
}
}
}
}
}
else{
echo "Query Failed";
}
?>

Check in your this
$query="select product_id from cart_details where cart_id = $cart_id";
That how many rows you are returning.
You can achieve this by changing this line if($result)
To this
if($result && mysqli_num_rows($result)!=0)
Note mysqli does not automatically secure your applicattion. use bindparam

Related

Getting the value from ($_GET["Value"])

// PAGE ONE
This is the index page, Here I am printing out rows from a list of
movie´s with some basic info, title , year.
I am also adding edit and delete links to the movies,
these work by passing the row id's of that movie.
$sql = "SELECT c.* , d.* FROM category c , movies d WHERE c.ID=d.ID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<div class='floatbox collection'><table><tr><th>Titel</th><th>regissör</th><th>År</th><th>Genre</th><th>Ändra</th><th>Ta bort</th>";
while($row = $result->fetch_assoc()) {
echo
"<tr><td>".$row["title"]."</td><td>".$row["director"]."</td><td>"
.$row["year"]."</td><td>".$row["category"]."</td>
<td><a href='edit.php?row=".$row["ID"]."'>justera</a></td>
// delete link to send ID to next page
<td><a href='delete.php?delete=".$row["ID"]."'>stryk</a></td>";
}
echo "</table></div>";
} else {
echo "0 results";
}
// PAGE TWO
Here I have my $row[ID] trough $_get by the link on the previous page,
I have checked the value by "dumping" so I know that the row ID is in that
variable: The thing is, How do I call that $ID in my sql statement?
I'm trying to delete by calling that ID on the table row.
// Variable with correct ID value
if(isset($_GET["ID"]))
if($_GET["ID"]) = $ID; // This doesn't work, Do I need to convert
the $get_ID to a variable with the ID that can be called in the statement?
or is my syntax wrong?
// Delete join
$sql = "SELECT * FROM movies, category
INNER JOIN category ON movies.ID = category.ID
DELETE WHERE movies.ID = '$ID'"; // No syntax works here
// Any help appreciated.
// konfirmering
if ($conn->query($sql) === TRUE) {
echo "Register struket <br>
<a href='panel.php'>Gå tillbaka</a>";
} else {
echo "Fel vid anslutning : " . $conn->error;
}
As per your code your delete.php would be like this:
// Variable with correct ID value
if (isset($_GET["delete"])) {
$ID = $_GET["delete"];
// Delete join
$sql = "DELETE FROM movies, category INNER JOIN category ON movies.ID = category.ID WHERE movies.ID = '$ID'";
// konfirmering
if ($conn->query($sql) === TRUE) {
echo "Register struket <br><a href='panel.php'>Gå tillbaka</a>";
} else {
echo "Fel vid anslutning : " . $conn->error;
}
}

Selecting Item From Category id

I'm trying to select items for my different category table but its shows only the i use with If() and I'm trying to use elseif
if($get_idy='Cars'){
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='Cars' ");
}elseif($get_idy='Trucks'){
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='Trucks' ");
}
while($row= mysqli_fetch_array($sel))
{
Can someone help?
You have put wrong if condition in your code.Single "=" assign the value in variable and double "==" check variable value is same or not. It should be like below.
if($get_idy=='Cars'){
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='Cars' ");
} elseif($get_idy=='Trucks'){
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='Trucks' ");
} else{
echo "The category is empty";
}
while($row= mysqli_fetch_array($sel))
{
}
You could have done like this instead checking condition for every category,
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='".$get_idy."'");
while($row= mysqli_fetch_array($sel)) {
//Your code here.
}
Please start using PDO as your sql is vulnerable to SQL Injections
Update: posted this update based on your comment
if(isset($get_idy)) {
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='".$get_idy."'");
while($row= mysqli_fetch_array($sel)) {
//Your code here.
}
} else {
echo "The category is empty";
}

How to show one image instead of four images

In Mysql database I have column name "product_id" and there is while statement code to check if row exist ...
If value of product_id exist, then it will show "note-icon.png" image, if not exit it will say "No" but the problem is that let say that in "product_id" has four product_id's SAME NUMBER and then result will SHOW FOUR images, my question is that how to write code to show ONE image instead of FOUR images!
<?php
$sql = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
$query = "SELECT * FROM notes WHERE product_id = $product_id";
$results = mysqli_query($sql, $query);
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
if(isset($row['$product_id'])) {
echo "<p>No </p>";
} else {
echo '<img src="images/note-icon.png" width="16" height="16" />';
} }
?>
Select only one row:
MySQL:
SELECT * FROM notes WHERE product_id = ? LIMIT 1
SQL:
SELECT FIRST 1 * FROM notes WHERE product_id = ?
If you want to get all notes but draw one image per product you can do this:
$product_ids = array();
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
if(!isset($row['product_id'])) {
echo "<p>No </p>";
} else {
if(!isset($product_ids[$row['product_id']])) {
$product_ids[$row['product_id']] = 1;
echo '<img src="images/note-icon.png" width="16" height="16" />';
}
}
}
Some code annotations:
$product_id in your query allowed, only if you are cast it to int before
else use placeholders (prepared statements)
if(isset($row['$product_id']))
What's this? think should be:
if(!isset($row['product_id']))
also you forgot to check $results, that it is not false, before using..
U can use grouping
SELECT * FROM notes WHERE product_id = ? GROUP BY product_id

PHP bug or logic error?

So here is my problem. I have a 2 tables. One for the Product details and One for the Product quantity.
In the product details there is a field Status it should be available or Not Available. Then in the Product quantity table there is a qtyleft meaning the quantity available for the product. Now the problem is i put 0 on quantity on 4 products meaning no quantity left and it should echo "Item is out of stock". It works but on 1 item only. I can't figure this out because I analyze everything and i think there is no problem. Is this a bug or what?
Here is my code:
<?php
if (isset($_GET['id']))
{
include('config.php');
$id=$_GET['id'];
$result = mysql_query("SELECT * FROM athan_products WHERE product_id = $id");
while($row3 = mysql_fetch_array($result))
{
$resultq = mysql_query("SELECT * FROM inventory WHERE product_id LIKE '%".$id."%'");
//$resultq = mysql_query("SELECT * FROM inventory WHERE product_id LIKE =$id");
while($rows = mysql_fetch_array($resultq))
{
$qwerty=$rows['qtyleft'];
}
if ($qwerty !=0){
echo '<tr>';
//echo '<td>'.$row3['product_size_name'].'</td>';
echo '<td>'.$row3['price'].'</td>';
echo '<td>'.$row3['description'].'</td>';
echo '<td>'.'<input name="but" type="image" value="'.$row3['id'].'" src="images/button.png" onclick="return myFunction()" />'.'</td>';
echo '</tr>';
}
else
{
echo '<tr>';
//echo '<td>'.$row3['product_size_name'].'</td>';
echo '<td align="center">'.'<h2>'.'Item is out of stock!'.'</td>';
echo '</tr>';
//echo '<td>'.'<h1>'.'"not available"'.'</h1>'.'</td>';
}
}
}
?>
I review your code, I think there is not any such error but you need to change you query.
$resultq = mysql_query("SELECT * FROM inventory WHERE product_id LIKE '%".$id."%'");
Into this which is given below.
$resultq = mysql_query("SELECT * FROM inventory WHERE product_id='".$id."'");
Hope you will solve your issue.
Thanks.

The stock was out of limit, still insert into order table

$prodqty = mysql_query("SELECT quan FROM pro_list WHERE auto_id = $pid"); //get the current product quantity
if (mysql_num_rows($prodqty) != 0)
{
$row = mysql_fetch_array($prodqty);
$productqty = $row['quan'];
}
$nqty = $productqty-$q; //current product quantity minus order quantity to get new product quantity
if ($nqty >= 0)
{
$query2="UPDATE pro_list SET quan = $nqty WHERE auto_id = $pid"; //update the quantity in the product table
$result = mysql_query($query2);
if ($result)
echo "Successfully ";
else
echo "Unsuccesfully";
}
else
echo "Limit of quantity! .";
}
die('Thank You For Shopping With i-Supply System! your order has been sent to Admin.!');
}
the stock was out of limits, but the orders still insert into database. the order should not insert into table order because it already out of limit the quantity of products. Customer should make an order again. Why this should happen?
Your approach is fundamentally flawed. You will get a race condition if two or more updates are attempted at the same time. You should perform the check and update as a single query like this:
$query="UPDATE pro_list SET quan=quan-$q WHERE auto_id=$pid and quan>=$q";
$result = mysql_query($query) or die(mysql_error());
if (mysql_affected_rows() == 0) {
echo "Out of stock!";
}
In any case, you shouldn't be using mysql - it's deprecated. Use mysqli or PDO instead.

Categories