我一直在寻找一种方法来在找到产品的结果后更新数据库的计数,但它会不断返回错误和/或不更新计数。我找到了正确更新计数的方法,但没有找到结果。如果我是瞎子请转发我。如果我忽略了任何事情,我很抱歉。 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> · <a href="../search">Search</a> · <a href="../application">Application</a> · <a href="../newuser">Register</a> · <a href="../contact_us">Contact Us</a> · <a href="../aboutus">About Us</a> · <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 :(得分:2)
更正1 : - 我认为您需要在while循环下面编写更新查询。因为您在创建之前使用$count
变量。
另请注明您是否需要为个别行或总计增加count
。
如果您需要更新单个行的计数,那么您在循环中有写入查询。
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);
}
?>
答案 1 :(得分: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());
?>
更具体一点 - 您试图致电:
$ucount = "UPDATE $usertable SET $count = ($count + 1)";
在你实际更新while循环中的$ count方法之前。