我正在使用一个名为Raspberry Pints的免费分发软件包来测量通过桶式水龙头的啤酒流量。我想更新一些功能,到目前为止做得很好。以下问题是目前让我头疼的问题。
工作原理: 我输入了一些啤酒,其中包含了许多细节(名称,颜色,酒精,ibu等等),它应该通过php更新sql表。如果使用.php脚本和包中发布的html表单一切正常。
我做了更改以添加新的啤酒特性,这需要更新大量文件并向sql表添加新列。我已经测试了新格式文件和旧格式文件的每种组合,并将我的问题缩小到下面的代码。目前,如果我使用默认条目表单,我可以在我的扩展表中创建一个新的啤酒条目而没有问题。只有在输入旧表格的啤酒后,我才能使用我的新表格进行更新并将最终变量输入表格。因为我可以在表中输入最终变量,所以我知道下面的代码是错误的。
任何人都可以确定我在哪里出错吗?基本上,如果啤酒存在,请更新其ID号。如果没有,请创建一个新条目。无论我到目前为止尝试过什么,我都无法创造新的条目。
类BeerManager {
function Save($beer){
$sql = "";
if($beer->get_id()){
$sql = "UPDATE beers " .
"SET " .
"name = '" . encode($beer->get_name()) . "', " .
"beerStyleId = '" . encode($beer->get_beerStyleId()) . "', " .
"notes = '" . encode($beer->get_notes()) . "', " .
"ogEst = '" . $beer->get_og() . "', " .
"fgEst = '" . $beer->get_fg() . "', " .
"srmEst = '" . $beer->get_srm() . "', " .
"ibuEst = '" . $beer->get_ibu() . "', " .
"water = '" . $beer->get_water() . "', " .
"salts = '" . $beer->get_salts() . "', " .
"finings = '" . $beer->get_finings() . "', " .
"yeast = '" . $beer->get_yeast() . "', " .
"modifiedDate = NOW() ".
"WHERE id = " . $beer->get_id();
}else{
$sql = "INSERT INTO beers(`name`, `beerStyleId`, `notes`, `ogEst`, `fgEst`, `srmEst`, `ibuEst`, `water`, `salts`, `finings`, `yeast`, `createdDate`, `modifiedDate`) " .
"VALUES(" .
"'" . encode($beer->get_name()) . "', " .
$beer->get_beerStyleId() . ", " .
"'" . encode($beer->get_notes()) . "', " .
"'" . $beer->get_og() . "', " .
"'" . $beer->get_fg() . "', " .
"'" . $beer->get_srm() . "', " .
"'" . $beer->get_ibu() . "' " .
"'" . $beer->get_water() . "' " .
"'" . $beer->get_salts() . "' " .
"'" . $beer->get_finings() . "' " .
"'" . $beer->get_yeast() . "' " .
", NOW(), NOW())";
}
mysql_query($sql);
}
--- --- EDITED
工作修复如下。我删除了所有并通过密钥重写密钥。经过比较后,我看到我添加的所有新变量都缺少逗号。这是因为我只是复制了get_ibu行,它不包含逗号作为NOW()
之前的最后一行
function Save($beer){
$sql = "";
if($beer->get_id()){
$sql = "UPDATE beers " .
"SET " .
"name = '" . encode($beer->get_name()) . "', " .
"beerStyleId = '" . encode($beer->get_beerStyleId()) . "', " .
"notes = '" . encode($beer->get_notes()) . "', " .
"ogEst = '" . $beer->get_og() . "', " .
"fgEst = '" . $beer->get_fg() . "', " .
"srmEst = '" . $beer->get_srm() . "', " .
"ibuEst = '" . $beer->get_ibu() . "', " .
"water = '" . $beer->get_water() . "', " .
"salts = '" . $beer->get_salts() . "', " .
"finings = '" . $beer->get_finings() . "', " .
"yeast = '" . $beer->get_yeast() . "', " .
"modifiedDate = NOW() ".
"WHERE id = " . $beer->get_id();
}else{
$sql = "INSERT INTO beers(name, beerStyleId, notes, ogEst, fgEst, srmEst, ibuEst, water, salts, finings, yeast, createdDate, modifiedDate ) " .
"VALUES(" .
"'". $beer->get_name() . "', " .
$beer->get_beerStyleId() . ", " .
"'". $beer->get_notes() . "', " .
"'". $beer->get_og() . "', " .
"'". $beer->get_fg() . "', " .
"'". $beer->get_srm() . "', " .
"'". $beer->get_ibu() . "', " .
"'". $beer->get_water() . "', " .
"'". $beer->get_salts() . "', " .
"'". $beer->get_finings() . "', " .
"'". $beer->get_yeast() . "', " .
"NOW(), NOW())";
}
mysql_query($sql);
}
答案 0 :(得分:0)
我不确定你编辑了多少代码,或者它与平台其他部分的接口如何,但在你的情况下,我认为值得查看" INSERT ... ON DUPLICATE KEY UPDATE语法& #34;这里记录了:http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
检查id的内容,你可以简单地运行一个命令,即
$sql = "INSERT INTO beers('name', 'beerStyleId', 'notes', 'ogEst', 'fgEst', 'srmEst', 'ibuEst', 'water', 'salts', 'finings', 'yeast', 'createdDate', 'modifiedDate') " .
"VALUES(" .
"'" . encode($beer->get_name()) . "', " .
$beer->get_beerStyleId() . ", " .
"'" . encode($beer->get_notes()) . "', " .
"'" . $beer->get_og() . "', " .
"'" . $beer->get_fg() . "', " .
"'" . $beer->get_srm() . "', " .
"'" . $beer->get_ibu() . "' " .
"'" . $beer->get_water() . "' " .
"'" . $beer->get_salts() . "' " .
"'" . $beer->get_finings() . "' " .
"'" . $beer->get_yeast() . "' " .
", NOW(), NOW())".
'ON DUPLICATE KEY UPDATE '.
"name = '" . encode($beer->get_name()) . "', " .
"beerStyleId = '" . encode($beer->get_beerStyleId()) . "', " .
"notes = '" . encode($beer->get_notes()) . "', " .
"ogEst = '" . $beer->get_og() . "', " .
"fgEst = '" . $beer->get_fg() . "', " .
"srmEst = '" . $beer->get_srm() . "', " .
"ibuEst = '" . $beer->get_ibu() . "', " .
"water = '" . $beer->get_water() . "', " .
"salts = '" . $beer->get_salts() . "', " .
"finings = '" . $beer->get_finings() . "', " .
"yeast = '" . $beer->get_yeast() . "', " .
"modifiedDate = NOW()";
答案 1 :(得分:0)
我的第一个猜测是beerStyleId
列包含文本,并且您不引用插入查询中的值。尝试将代码更新为:
$sql = "INSERT INTO beers(`name`, `beerStyleId`, `notes`, `ogEst`, `fgEst`, `srmEst`, `ibuEst`, `water`, `salts`, `finings`, `yeast`, `createdDate`, `modifiedDate`) " .
"VALUES(" .
"'" . encode($beer->get_name()) . "', " .
"'" . encode($beer->get_beerStyleId()) . "', " .
"'" . encode($beer->get_notes()) . "', " .
"'" . $beer->get_og() . "', " .
"'" . $beer->get_fg() . "', " .
"'" . $beer->get_srm() . "', " .
"'" . $beer->get_ibu() . "' " .
"'" . $beer->get_water() . "' " .
"'" . $beer->get_salts() . "' " .
"'" . $beer->get_finings() . "' " .
"'" . $beer->get_yeast() . "' " .
", NOW(), NOW())";
我还建议您通过在脚本顶部添加以下内容来启用错误报告:
ini_set('display_errors', true);
error_reporting(E_ALL);
请注意,您不应使用旧的mysql_
扩展程序,因为已弃用,请切换为mysqli
或pdo
。