[英]MySQL query: uses previous values from the table, when previous value exists. Needs to be fixed when there are still no previous values in the table
$query = "INSERT INTO temperature (city, change, curr_temp) SELECT city, '".$cleaned_change."', curr_temp+'".$cleaned_change."' FROM temperature where city='".$chosen_city."' order by Time desc, id desc limit 1";
So, each row displays city, changes of temperature and current temperature. 因此,每一行显示城市,温度和当前温度的变化。 If we have $cleaned_change=2 (Forengheits) change for San Francisco and its curr_temp was 47F (taken from the previous row using SELECT), we insert ('San Francisco', '2', '49').
如果我们在旧金山有$ cleaned_change = 2(Forengheits)更改,并且其curr_temp为47F(使用SELECT从上一行获取),则我们插入('San Francisco','2','49')。 (was 47F, change is +2F, now it is 49F).
(以前是47F,更改是+ 2F,现在是49F)。
The problem is: if that's the first row it doesn't work, because we have no 47F to be selected (there are no previous values). 问题是:如果那是第一行,它将不起作用,因为我们没有要选择的47F(没有以前的值)。 As result, it doesn't insert a row.
结果,它不会插入行。 How to fix it?
如何解决?
The good idea is to insert the very first row "manually" (with a temperature of 0F or 32F or whatever). 好的主意是“手动”插入第一行(温度为0F或32F或其他温度)。 And that works fine.
而且效果很好。 but is there any possible way to check if curr_temp has a previous value?
但是有什么方法可以检查curr_temp是否具有先前的值? I want it work from the scratch (when the table is truncated, it stops working) and using just one query.
我希望它从头开始工作(当表被截断时,它将停止工作)并且仅使用一个查询。
PS There are NO nulls. PS没有空值。 If row exists, curr_temp has a numeric value in the Table.
如果存在行,则curr_temp在表中具有数字值。 Thank you.
谢谢。
PSS I forgot to notice that I have slightly simplified the query. PSS我忘了注意到我稍微简化了查询。 Yes, it cleans the POST values from possible injections.
是的,它将清除可能的进样中的POST值。 But, thank you for saying that.
但是,谢谢你这么说。
Try making just a SELECT query, then do your PHP logic, then use an INSERT query. 尝试仅执行SELECT查询,然后执行PHP逻辑,然后使用INSERT查询。 Then you can build in protection against SQL injection and have a simpler time with your logic.
然后,您可以建立防止SQL注入的保护措施,并缩短逻辑时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.