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

时间:2014-11-26 08:09:20

标签: php mysql

这是我的代码。它一直有效,直到我得到比较位(参见代码中的注释)。有人可以帮忙吗?

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

我想要实现的注意事项在代码中。有人可以帮忙吗?

基思G. 澳大利亚布里斯班

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

isset(如果$ temperature&gt; $ qwert)

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