I'm working with a freely distributed package called Raspberry Pints to measure flow of beer through keg taps. I wanted to update a few of the features and have done quite well so far. The following problem is all that is currently making me scratch my head.
How it works: I input a beer with a number of specifics for the brew (name, color, alcohol, ibu, etc...) and it is supposed to update a sql table via php. If is use the .php script and html forms as released in the package everything works.
I made changes to add new beer characteristics which required updating a lot of files and adding new columns to the sql table. I've tested every combination of new vs. old format files and have narrowed my issues down to the code below. Currently if I use the default entry form I can create a new beer entry into my extended table without problem. Only after entering the beer with old form can I use my new form to do update and enter the final variables into the table. Because I can enter the final variables into the table I know the code below is at fault.
Can anyone identify where I'm going wrong in it? Basically if the beer exists, update its ID number. If not, make a new entry. I cannot make a new entry no matter what I've tried thus far.
class 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---
Working fix is below. I deleted all and re-wrote key by key. After comparison I saw that commas were missing for all new variables I added in. This is because I simply copied the get_ibu line which didn't contain a comma as the last line before 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);
}