问题
I have a little temperature protocoling web app, where degrees get adjusted on a page and saved in a database.
It works, but sometimes sporadic errors occur from users which I can't reproduce!
Sometimes there are null-entries in the database and I don't know how and why. It "nulls" the hole row (several temperatures per day are selectable).
If e.g. no temp got selected in a div string "NA" should be handed to the db. This bug is so resilient, I don't know where else to turn to.
Ajax increases Values by clicking on Arrow
$(".arrow_up").die('click');
$(".arrow_up").live('click',function() {
var value = $("#temp_"+this.id).html();
if (value=="NA") {value=$("#istemp_"+this.id).html()}
var newValue = parseInt(value)+ 1;
hands Values over to server
$(document).ready(function() {
$('#save').die('click');
$("#save").live('click',function() {
var tempA=$("#temp_itemA").html();
var tempB=$("#temp_itemB").html();
PHP has to store it
I look if it isn't empty nor that the string null gets handed over. Last one throws Error.
<?php
if((!isset($_GET['tempA'])) || (!isset($_GET['tempB'])) || ($_GET['tempA']!='null')
{
echo "values are missing";
}
else
{
error_reporting(E_ALL);
try {
$dbh = new PDO('sqlite:/var/www/xxx.sqlite');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$date=date("Y-m-d",time());
$sql = "SELECT * FROM tableTemp WHERE date='".$date."'";
$result = $dbh->query($sql);
foreach($result as $row) {
$id = $row['id'];
}
if (!empty($id)) {
$sql = "UPDATE tableTemp SET tempA='".$_GET['tempA']."',tempB='".$_GET['tempB']"'";
$result = $dbh->query($sql);
}
else {
$sql = "INSERT INTO tableTemp ('tempA','tempB')";
$result = $dbh->query($sql)
?>
I mean, this is as simple as it gets. I can only imagine the trasmitting errors might have something to do with WIFI and packet loss.
How can I can stop inserting (discarding) these pesky null string into the db and throw an error, so that the user has to try it again? My !isset does not work since it isn't empty
回答1:
Note this is probably stating the obvious
But can you not stipulate that the column in the DB requires a value that is not null then catch the error on the insert
have you seen this answer?
https://stackoverflow.com/a/2103256/1479565
来源:https://stackoverflow.com/questions/11211127/sporadic-unwanted-null-entries-in-database-get