PHP bug or logic error? - php

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.

Related

Displaying all items from mysql database

I have table product and table category in my database. I want to display all category dynamically in a table and inside the table of each category I am displaying all item belonged to that category too.
Those categories and items should be displayed like this :
And here is my coding to do the work :
$fetch_cat = "SELECT * FROM tblcat"; //fetch from table category
$result = $conn->query($fetch_cat);
if ($result->num_rows > 0)
{
while($cat_row = mysqli_fetch_assoc($result))
{
$cat_title = $cat_row['catName'];
echo '<table>';
echo '<tr>';
echo '<td><img src="category/'.$cat_row['catImg'].'" /></td>';
echo '<td>';
echo '<ul class="content_init">';
$stmt = "SELECT * FROM tblproduct WHERE prodCat = '".addslashes($cat_title)."' LIMIT 4"; //fetch from table product
$result = $conn->query($stmt);
if ($result->num_rows > 0)
{
while($row = mysqli_fetch_assoc($result))
{
echo '<li>';
echo '<a href="#"><img style="height: 188px; width: 188px;" src="user_images/'.$row['prodImg'].'" />';
echo '<br /><br />';
echo '<h4>'.$row['prodName'].'</h4>';
echo '<label><span>RM </span>'.$row['prodPrice'].'</label></a>';
echo '</li>';
}
}
echo '</ul>';
echo '</td>';
echo '</tr>';
echo '</table>';
}
}
Problem :
So the problem with my coding is, it can only display Category 1 with items belonged to it. The rest categories unable to be displayed. I guess my coding might not loop properly because of bad programming as it unable to display the expected output.
The $result variable was reused by the inner query thus the first result override.
Change the variable name either of the $result variable.
$result1 = $conn->query($stmt);
if ($result1->num_rows > 0)
{
while($row = mysqli_fetch_assoc($result1))
{
Your approach is not good/optimal. You have query for the categories and then one query per each category,so if you have 10 categories your code would execute 11 queries. You should do it with one query only using INNER JOIN
You can select all the values left joining the category table for the names and group them by their category ID.
See Group array by subarray values
Afterwards, you can traverse each product for each category by accessing the subarrays.

Can Mysqli_query() calls be nested?

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

Same query, different results in php vs phpmyadmin- Why?

I am trying to get the total actions from each employee in a MySQL database. My script is giving me a significantly lower total than the number in the database for all employees. I am using a SUM function in the query. The query works fine in phpmyadmin, but not in my script. Any ideas why this is happening.
$query = "SELECT user_id, SUM(num_actions) as action FROM pro_actions GROUP BY user_id ORDER BY action DESC";
if ($result = $db->query($query)) {
$count = 0; // this is the total of all employee actions. which adds up correctly!
while ($row = mysqli_fetch_array($result)) {
$count += $row['action'];
echo '<tr><td>';
echo $row['user_id'];
echo '</td><td>';
echo $row['action'];
echo '</td></tr>';
}
$result->free();
}
When I run this script, employee 1005 has 63 actions. However, when I run this query in phpmyadmin, employee 1005 has 194 actions (which is correct). All employees have fewer actions in the output of the script. The interesting thing is that the $count variable outputs the correct amount, which is the total of all actions... Please help with this glitch.
$query = "SELECT user_id, SUM(num_actions) as action FROM pro_actions GROUP BY user_id ORDER BY action DESC";
if ($result = $db->query($query)) {
$count = 0; // this is the total of all employee actions. which adds up correctly!
while ($row = mysqli_fetch_array($result)) {
$count += $row['action'];
echo '<tr><td>';
echo $row['user_id'];
echo '</td><td>';
echo $row['action'];
echo '</td></tr>'; //tag mismatch
}
$result->free();
}

Pagination return incorrect amount of pages

I've built a pagination script based on this website http://tuts.wtfdiary.com/2012/06/simple-pagination-using-php-with-css.html
I've altered it slightly but it's giving me a little trouble. If the results are spread over more than 40 entries, it's supposed to be 10 per page so theoretically it should show 5 pages right? But quite often it'll show more than this.
Would anybody mind point me in the right direct? My code's below....
thanks guys :)
<?php
//include ("db.php");
include_once 'header.php';
$ddoption='';
if (isset($_GET['searchid'])){
$searchid = $_GET['searchid'];
}
//$result=mysql_query("select count(*) from product");
$result=mysql_query("select count(*) from product WHERE (title OR description LIKE '%".$searchid."%')OR(title ='".$searchid."' ) OR(description='".$searchid."')");
$row=mysql_fetch_row($result);
$tr=$row[0];
$rpp=10;
$pn=1;
$searchid = '';
if (isset($_GET['searchid'])){
$searchid = $_GET['searchid'];
}
if(isset($_GET['pn']))
{
$pn=$_GET['pn'];
}
$oldtp=($tr/$rpp);
$tp=round($oldtp);
if($tr%$rpp>0)
{
$tp++;
}
$from=(($pn-1)*$rpp)/*+1*/;
$to=($pn)*($rpp)-1;
//$result=mysql_query("SELECT * from product WHERE productid between $from AND $to");
//$result=mysql_query("SELECT * FROM product WHERE productid between $from AND $to AND title OR description LIKE '%".$id."%'" );
echo "<h1> Results from ". $from." to ".$to. "</h1>";
if(isset($_GET['option'])){
$ddoption = $_GET['option'];
if($ddoption=="both"){
$result=mysql_query("SELECT * FROM product WHERE (title LIKE '%".$searchid."%')OR (description LIKE '%".$searchid."%') OR(title ='".$searchid."' ) OR(description='".$searchid."') ORDER BY productid DESC LIMIT $from,10");
}
else if($ddoption=="title"){
$result=mysql_query("SELECT * FROM product WHERE (title LIKE '%".$searchid."%')OR(title ='".$searchid."' ) ORDER BY productid DESC LIMIT $from,10");
}
else{
$result=mysql_query("SELECT * FROM product WHERE (description LIKE '%".$searchid."%')OR(description='".$searchid."') ORDER BY productid DESC LIMIT $from,10");
}
}
echo "<table>";
while($row=mysql_fetch_array($result))
{
echo '<tr><td>'.'<img src="'.$row['image1'].'" alt="'.$row['title'].'" height="100" width="100"/></td>';
echo '<td>'.$row['title'].'</td></tr>';
}
echo "</table>";
echo "<ul id='pages'>";
for($i=1;$i<=$tp;$i++)
{
echo "<li><a href='search.php?pn=$i&searchid=$searchid&option=$ddoption'>Page $i</a></li>";
}
echo "</ul>";
echo <<<_END
</body>
</html>
_END;
?>
you can count your pages in one line:
$tp = ceil($tr/$rpp);
instead of
$oldtp=($tr/$rpp);
$tp=round($oldtp);
if($tr%$rpp>0)
{
$tp++;
}
possible reason of different number of rows is that you queries are different when counting rows and when displaing them.

populate a drop down from a table and then set the selected option from another table

I have a table of order statuses that a sales order can have; the statuses are not necessarily sequential. I can get these statuses into a drop down using the following:
$sql1 = 'SELECT * FROM `mjj_order_status` ORDER BY Order_status_name DESC';
$result_order_status = mysql_query($sql1);
echo '<select name="Order_status">';
while ($row = mysql_fetch_array($result_order_status)) {
echo '<option
value="'.$row['Order_status_ID'].'">'.$row['Order_status_name'].'</option>';
}
echo '</select>';
My problem occurs when I try to fetch the order data from the DB, and then set the dropdown list to that order's status. I can run the query to get the order status that should be selected, but am not sure how to code how to apply this information. The query to get the order status ID is:
SELECT Order_status FROM `mjj_orders` WHERE Order_ID = $_POST['Order_ID']
I have searched (I think comprehensively) and I haven't found a feasible answer or - to be fair - one that I can understand.
(I have found a question similar to this one, but I am unsure how to get them to elaborate... selected item ind dropdown when editing).
Any and all advice would be great. Thanks.
just drop in some logic checking what status the order you are working on has.
$sql = "SELECT Order_status FROM 'mjj_orders' WHERE Order_ID = ".$_POST['Order_ID'];
$result=mysql_query($sql);
$myorder=mysql_fetch_array($result);
$sql1 = 'SELECT * FROM `mjj_order_status` ORDER BY Order_status_name DESC';
$result_order_status = mysql_query($sql1);
echo '<select name="Order_status">';
while ($row = mysql_fetch_array($result_order_status)) {
if($myorder['Order_status']==$row['Order_status_ID']){
$selectCurrent=' selected';
}else{
$selectCurrent='';
}
echo '<option value="'.$row['Order_status_ID'].'" '.$selectCurrent.'>'.$row['Order_status_name'].'</option>';
}
echo '</select>';
just mark the option where order_status_id = the id that should be selected with selected. eg. the option that will be selected will look like <option selected value="... like this:
$id_to_select = // Your logic to get the id to select
$sql1 = 'SELECT * FROM `mjj_order_status` ORDER BY Order_status_name DESC';
$result_order_status = mysql_query($sql1);
echo '<select name="Order_status">';
while ($row = mysql_fetch_array($result_order_status)) {
if($id_to_select == $row['Order_status_ID'])
echo '<option selected value="'.$row['Order_status_ID'].'">'.$row['Order_status_name'].'</option>';
else
echo '<option value="'.$row['Order_status_ID'].'">'.$row['Order_status_name'].'</option>';
}
echo '</select>';

Categories