[英]update mysql database after condition satisfied in PHP
This is my code. 这是我的代码。 It works until I get down to the comparison bit (see the notes in the code). 直到我进入比较位(见代码中的注释),它才起作用。 Can anyone help, please? 有人可以帮忙吗?
<?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);
?>
The notes of what I am trying to achieve are in the code. 我试图实现的注释在代码中。 Can anyone please help? 谁能帮忙吗?
Keith G Brisbane, Australia Keith G布里斯班,澳大利亚
// You are missing semi-colon here. //您在这里缺少分号。 Also, add single quotes for values. 另外,为值添加单引号。
$result = mysql_query("UPDATE mystats SET mnhitempc = '$temperature', mnhitempdt = '$date', mnhitemptm = '$time' WHERE id=5");
isset(if $temperature > $qwert) isset(如果$ temperature> $ qwert)
Use isset() function to check condition if it's set or not , then update MySQL database 使用isset()函数检查条件是否已设置,然后更新MySQL数据库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.