0

for eg: I had 3 values in table item:

car <br>
bike<br>
cycle<br>

In product page when clicking on any product it will pass value to next page and store it as variable, I want to find the sum of item that passed from another page

the code is here.

<?php
    if(isset($_GET['id'])){
        $id = $_GET['id'];
        $qry=mysql_query("SELECT * FROM addingre WHERE id=$id", $con);
        if(!$qry){
            die("Query Failed: ". mysql_error());
        }

        while($row=mysql_fetch_array($qry)){
            $ttt= "<h2>".$row['ingredients']."</h2>";
        }
    }
?>
<?php
    echo "$ttt";
?>

<?php
    $query_check_credentials = "SELECT product, SUM(quantity) AS total_amount FROM ingredients WHERE item = '$ttt' GROUP BY product";

    $res = mysql_query($query_check_credentials);

    while($row = mysql_fetch_array($res)){
        echo $row['total_amount'];
    }
?>
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • 8
    This question seems very unclear. Please edit the question fully explaining the goal, what you've tried, and where exactly it is going wrong. – mickmackusa Feb 18 '17 at 16:28
  • yes ..like said by mickmackusa please explian better your question – ScaisEdge Feb 18 '17 at 16:29
  • In case English isn't your mother tongue, here's a guess: Do you want to do a "grand total' of the sums in addition to the individual product sums? If so, you can just use php in your while loop `$total+=$row['total_amount'];` – mickmackusa Feb 18 '17 at 16:38
  • english isn't my mother tongue, for me it is very hard to explain @mickmackusa – RiyAs MuhaMmed Feb 18 '17 at 16:55
  • edited question,please look now – RiyAs MuhaMmed Feb 18 '17 at 17:01
  • Sorry, I'm still not following. Can you explain exactly where your code breaks/fails? Is `$row['total_amount']` giving a result? the correct result? Is the query returning any rows in the result set? or is there an error? Please give more information. – mickmackusa Feb 18 '17 at 17:03
  • when i put on the line where item =' bike ' it shows correct answer, but when i try where item = '$ttt' it didnt show any thing, blank page, no errors @mickmackusa – RiyAs MuhaMmed Feb 18 '17 at 17:07
  • It looks like you aren't doing any error detection on your query. I see that you are wrapping $ttt in

    tags. When you echo $ttt is will format the text and the tag syntax will disappear. As for the query, if you are seaching for 'bike', but feeding '

    bike

    – mickmackusa Feb 18 '17 at 17:13
  • If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Feb 20 '17 at 21:29

1 Answers1

0

Do not add <h2></h2> when first declaring the $ttt variable.

Leave it bare. e.g. 'bike'

Only add the tags when echoing. e.g. echo "<h2>$ttt</h2>";

This will allow you to reuse $ttt in the query.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136