我对下面的函数感到非常沮丧。它不会更新我在数据库中的标志。我试过把1和0放在引号之间,而不是。我在数据库中将字段设置为小整数。你看到我做错了什么吗?该条目已存在,但1未更新为0。
function postValue(){
global $customerID;
$query="SELECT flag FROM welcomecall WHERE customerID= '$customerID'";
echo $query;
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "The customer flag is ". $row["flag"];
}
if ($row['flag']=='0'){
$query="UPDATE welcomecall SET flag='1' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
else if($row['flag']=='1'){
$query="UPDATE welcomecall SET flag='0' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
}
当while
表达式退出时,将不剩下$row
:
while ($row = mysql_fetch_assoc($result)) {
所以这不会是真的:
if ($row['flag']=='0'){
考虑在while
循环中移动if
语句。
看起来您在循环之外使用的逻辑属于循环内部:
function postValue(){
global $customerID;
$query="SELECT flag FROM welcomecall WHERE customerID= '$customerID'";
echo $query;
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "The customer flag is ". $row["flag"];
// Moved
if ($row['flag']=='0'){
$query="UPDATE welcomecall SET flag='1' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
else if($row['flag']=='1'){
$query="UPDATE welcomecall SET flag='0' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
}
}