Hello I wonder if someone please can help me get this to work? :/ <? //räknar ihop bokade kviga biljetter $query = "SELECT sum( kviga ) FROM `bokning`"; //kollar limit på kviga billjetter $query2 = "SELECT sum( kvigamax ) FROM `admin`"; $result = mysql_query($query) or die(mysql_error()); $result2 = mysql_query($query2) or die(mysql_error()); // Print out result of kviga while($r = mysql_fetch_row($result)) { echo $r[0]; } // Print out result of kvigamax while($r2 = mysql_fetch_row($result2)) { echo $r2[0]; } if($r[0] + $_POST[kviga] < $r2[0]) { $sql="INSERT INTO bokning (lagnamn, lagledare, kviga, kalv, ko, deltagare, ovrigt) VALUES ('$_POST[lagnamn]','$_POST[lagledare]','$_POST[kviga]','$_POST[kalv]','$_POST[ko]','$_POST[deltagare]','$_POST[ovrigt]')"; if (!mysql_query($sql,$db)) { die('Error: ' . mysql_error()); } echo "Bokningen är nu genomförd"; mysql_close($db) } else { echo "cant do that"; } ?> PHP:
I don't quite get what you are trying to but I suspect that this line is where its breaking $sql="INSERT INTO bokning (lagnamn, lagledare, kviga, kalv, ko, deltagare, ovrigt) VALUES ('$_POST[lagnamn]','$_POST[lagledare]','$_POST[kviga]','$_POST[kalv]','$_POST[ko]','$_POST[deltagare]','$_POST[ovrigt]')"; PHP: and it needs to become $sql="INSERT INTO `bokning` (`lagnamn`, `lagledare`, `kviga`, `kalv`, `ko`, `deltagare`, `ovrigt`) VALUES ('{$_POST[lagnamn]}','{$_POST[lagledare]}','{$_POST[kviga]}','{$_POST[kalv]}','{$_POST[ko]}','{$_POST[deltagare]}','{$_POST[ovrigt]}')"; PHP: however you should also be putting addslashes around those post variables if they are not gained from a secure location. The quickest (and dirtiest) way is foreach($_POST) as $k=>$v) $_POST[$k] = addslashes($v); PHP:
ah, i se i forgot to ad the problem to my post. What you stated above to work, thanks anyway. my problems is that the "if" statement wont work, if($r[0] + $_POST[kviga] < $r2[0]). i can change the less value to either a "<" or a ">" and the result will still be "cant do that". in my database the sum of "$query2" is 200, and the sum of "$query" is 55. "$r[0] + $_POST[kviga]" should together not be over 200 or the else statement should kick in.
You need to echo out some of those values and check they are what you are expecting. eg echo $r2[0] . '<br>'; echo $_POST[kviga] . '<br>'; echo $r[0] . '<br>'; PHP:
i think the problem is $r is empty after while statement. try this : $row=0; $row2=0; while($r = mysql_fetch_row($result)) { echo $r[0]; $row+=$r[0]; } // Print out result of kvigamax while($r2 = mysql_fetch_row($result2)) { echo $r2[0]; $row2+=$r2[0]; } if(($row + $_POST[kviga]) < $row2) PHP: