繁体   English   中英

在PHP中满足条件后更新mysql数据库

[英]update mysql database after condition satisfied in PHP

这是我的代码。 直到我进入比较位(见代码中的注释),它才起作用。 有人可以帮忙吗?

<?php
$time = '10:00a';
$date = '26-11-14';
$temperature = 29.3;

                require_once 'login.php';
                $db_server = mysql_connect($db_hostname, $db_username, $db_password);
                if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
                mysql_select_db($db_database)
                    or die("Unable to select database: " . mysql_error());
$result = mysql_query("SELECT * FROM mystats WHERE id=5"); 
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
 }
$row = mysql_fetch_array($result);
echo $row[mnhitempc];
echo $row[mnhitempdt];
echo $row[mnhitemptm];
    $qwert = $row[mnhitempc];
    echo $qwert;
//$qwert is the temperature in the SQL Database and $temperature is the current    temperature
//What I am trying to do is to compare the 2 temperatures and if the current is > that in the database 
//update the database with that temperature and the date and time.  THE NEXT 2 LINES ARE WHAT IS NOT WORKING.   
    if ($temperature > $qwert)
    {$result = mysql_query("UPDATE mystats SET mnhitempc = $temperature, mnhitempdt =  $date, mnhitemptm = $time WHERE id=5") 
                //$result = mysql_query($query);
                {if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
}
}
mysql_close($db_server);
?>

我试图实现的注释在代码中。 谁能帮忙吗?

Keith G布里斯班,澳大利亚

//您在这里缺少分号。 另外,为值添加单引号。

$result = mysql_query("UPDATE mystats SET mnhitempc = '$temperature', mnhitempdt =  '$date', mnhitemptm = '$time' WHERE id=5");

isset(如果$ temperature> $ qwert)

使用isset()函数检查条件是否已设置,然后更新MySQL数据库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM