rmushero
rmushero

Reputation: 39

Update count after returning result

I was looking for a way to update the count for the database after finding the result of a product but it keeps returning an error and/or not updating the count. I found ways to update the counts out right but not after finding a result. If i'm blind please redirect me. My apologies if I overlooked anything. RAM

<?php

//Variables for connecting to your database.
//These variable values come from your hosting account.


require_once '../connect/connect.php';

$usertable = "UPC_Product";
$yourfield2 = "Product";
$yourfield1 = "UPCA";
$yourfield3 = "Gluten Free";
$yourfield4 = "Company";
$yourfield5 = "search_cnt";
$yourfield6 = "CompName";
$yourfield7 = "id";
$search_id = $_POST["search_id"];

//Connecting to your database
mysql_connect($hostname, $username, $password) or die("Unable to
            connect to database! Please try again later.");
mysql_select_db($dbname);

//Fetching from your database table.
$query = "SELECT * FROM $usertable WHERE UPCA = $search_id";
$result = mysql_query($query);

if ($result) {
    while ($row = mysql_fetch_array($result)) {
        $name = $row["$yourfield2"];

        $upca = $row["$yourfield1"];

        $gluten = $row["$yourfield3"];

        $count = $row["$yourfield5"];

        $id = $row["$yourfield7"];
    }

//update count 
    $ucount = "UPDATE $usertable SET search_cnt = ($count + 1)";
    mysql_query($ucount);
}
?>


<head>
<link rel="icon"
      type="image/png"
      href="../images/GluteFreefavicon.jpg">
<meta name="viewport" content="width=device-width">
<title>Result Page</title>
<meta http-equiv="Content-Language" content="English" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="/style.css" media="screen" />
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-39600334-1', 'glutefree.com');
  ga('send', 'pageview');

</script>
</head>
<body>
<div id="wrap">
    <div id="top"></div>
    <div id="content">
        <div class="header">
<a href="index.html"><img src="/images/glutefreefacebookbanner.jpg"/></a>

        </div>
        <div class="breadcrumbs">
            <center> <a href="index.html">Home</a> &middot; <a href="../search">Search</a> &middot; <a href="../application">Application</a> &middot; <a href="../newuser">Register</a> &middot; <a href="../contact_us">Contact Us</a> &middot; <a href="../aboutus">About Us</a> &middot; <a href="">Games</a>

            </center>
        </div>
        <div class="result">
            <center>Your Results
                <br/>
            </center>Product:
            <?php echo $name; ?>
            <br/>
            <div class="product">Gluten Free?
                <?php echo $gluten; ?>
                <br>
                <br>UPC:
                <?php echo $upca;?>
                <br>Search count:
                <?php echo $count;?>
                <br/>
            </div>
        </div>
        <p>No Result? Click <a href="../submit_error.html">here</a> to fill out information on the product you were searching for.</p>
    </div>
    <div id="bottom"></div> 
</div> 
<div id="footer">
    </a>
</div> 
</body>
</html>

Upvotes: 1

Views: 288

Answers (2)

Roopendra
Roopendra

Reputation: 7776

correction 1 :- I think you need to write your update query below the while loop. because you are using $count variable before creating it.

Also please specify if you need to increase count for individual row or total.

If you need to update count for individual row then you have write query inside while loop.

UPDATE $usertable SET search_cnt = ($count + 1) WHERE id_column_name = $id;

correction 2:- You are calling mysql_query($ucount); in wrong place. this should be below update query.

Correction 3 :- as $count is your count number then obviously it will not be your column name , write down you column name in update query.

UPDATE $usertable SET count_column_name = ($count + 1)

Corrected Block of code:-

<?php

//Variables for connecting to your database.
//These variable values come from your hosting account.


require_once '../connect/connect.php';

$usertable = "UPC_Product";
$yourfield2 = "Product";
$yourfield1 = "UPCA";
$yourfield3 = "Gluten Free";
$yourfield4 = "Company";
$yourfield5 = "search_cnt";
$yourfield6 = "CompName";
$yourfield7 = "id";
$search_id = $_POST["search_id"];

//Connecting to your database
mysql_connect($hostname, $username, $password) or die("Unable to
            connect to database! Please try again later.");
mysql_select_db($dbname);

//Fetching from your database table.
$query = "SELECT * FROM $usertable WHERE UPCA = $search_id";
$result = mysql_query($query);

if ($result) {
    while ($row = mysql_fetch_array($result)) {
        $name = $row["$yourfield2"];

        $upca = $row["$yourfield1"];

        $gluten = $row["$yourfield3"];

        $count = $row["$yourfield5"];

        $id = $row["$yourfield7"];
    }

//update count
    // Please write here count column name not $count. I have write count at this time. 
    $ucount = "UPDATE $usertable SET search_cnt = ($count + 1)";
    mysql_query($ucount);
}
?>

Upvotes: 2

BuddhistBeast
BuddhistBeast

Reputation: 2670

All you need to do is rearrange a function to get the correct count. Below should be the right code.

<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.


require_once '../connect/connect.php';

$usertable = "UPC_Product";
$yourfield2 = "Product";
$yourfield1 = "UPCA";
$yourfield3 = "Gluten Free";
$yourfield4 = "Company";
$yourfield5 = "search_cnt";
$yourfield6 = "CompName";
$yourfield7 = "id";
$search_id = $_POST["search_id"];

//Connecting to your database
mysql_connect($hostname, $username, $password) or die ("Unable to
            connect to database! Please try again later.");
mysql_select_db($dbname);

//Fetching from your database table.
$query = "SELECT * FROM $usertable WHERE UPCA = $search_id" ;
$result = mysql_query($query);
if ($result) {
    while($row = mysql_fetch_array($result)) {
        $name = $row["$yourfield2"];

        $upca = $row["$yourfield1"];

        $gluten = $row["$yourfield3"];

        $count = $row["$yourfield5"];

        $id = $row["$yourfield7"];

}
}
$ucount = "UPDATE $usertable SET $count = ($count + 1)";
mysql_query($ucount) or die(mysql_error());
?>

To be more specific - You were trying to call:

$ucount = "UPDATE $usertable SET $count = ($count + 1)";

before you actually updated the $count method in the while loop.

Upvotes: 0

Related Questions