2013-12-23 107 views
1

我正在寻找一种方法来查找产品的结果后更新数据库的计数,但它一直返回一个错误和/或不更新计数。我找到了更新计数的方法,但没有找到结果。如果我瞎了,请重定向我。如果我忽视任何事情,我很抱歉。 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> 
+0

什么是$计数? – Roopendra

+0

你会得到什么错误? –

+0

它与css有什么关系? – NoobEditor

回答

2

校正1: - 我认为你需要编写while循环下面的更新查询。因为在创建它之前您正在使用$count变量。

另请注明您是否需要增加个别行或总数的count

如果您需要更新单个行的计数,那么您在while循环内写入查询。

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

修正2: - 你在错误的地方调用mysql_query($ucount);。这应该低于update查询。

更正3: - 作为$count是你的计数,那么显然它不会是你的列名,在更新查询中写下你的列名。

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

修正区块的代码: -

<?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); 
} 
?> 
+0

检查我更新的答案 – Roopendra

+0

你们是在回答这些问题,但它仍然无法正常工作。它可能是服务器端防止它无法工作的东西? – rmushero

+0

你的计数列名是什么? – Roopendra

0

所有你需要做的是重新安排一个函数来获取正确的计数。下面应该是正确的代码。

<?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()); 
?> 

更具体 - 你试图拨打:你居然更新了while循环的$计数方法

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

之前。

+0

使用它不会再抛出错误,但它仍然不会更新数据库中的计数:( – rmushero

+0

对不起,我忘了再次调用查询了。上面的代码现在可以工作,它应该更新您的数据库:) – BuddhistBeast

+0

代码中的语法错误。 – Roopendra