Tje Posted August 8, 2014 Share Posted August 8, 2014 Hi, I'm trying to insert calculate values to DB.this is my code <?php require('dbconnection.php'); $a=$_POST['a']; $b=$_POST['b']; class cal{ public function insert($a,$b){ if((($a)&&($b))!="") { $c=$a*$b; $sql = "INSERT INTO cropplant (sbetweenQ ,sbetweenC ,sbetweenTC ) VALUES ('$a', '$b', '$c')"; $query = mysql_query($sql); if(!$query) { echo '<script type="text/javascript">alert("DB Update error ! please Re-enter your details.");</script>'; }else { echo '<script type="text/javascript">alert("New Cropplant Added Successfuly.");</script>'; } }else{ echo"please enter all"; } } } ?> what is the error in this code.please help Quote Link to comment https://forums.phpfreaks.com/topic/290346-how-to-insert-calculate-values-to-database/ Share on other sites More sharing options...
mac_gyver Posted August 8, 2014 Share Posted August 8, 2014 in general, you wouldn't put derived values into a database, but assuming your code is just an example/exercise, you didn't show us the code that's making an instance of your cal class and calling the ->insert() method. also, you should not be using the msyql_ database functions because they are obsolete and will be removed in a future php version, which is interesting because at least one of your earlier threads in this forum was using the mysqli_ database functions. Quote Link to comment https://forums.phpfreaks.com/topic/290346-how-to-insert-calculate-values-to-database/#findComment-1487172 Share on other sites More sharing options...
requinix Posted August 8, 2014 Share Posted August 8, 2014 (edited) 1. Not checking if the "a" and "b" POST values are present 2. Defining a class in a file alongside (I think?) regular code 3. Using variable names that have no intrinsic meaning 4. Random indentation style 5. Not sanitizing/validating the form input and/or the a and b values 6. Putting output inside a method when it should be returning success or failure 7. Not actually calling any of the insert() code 8. Using mysql extension instead of PDO or mysqli or something else Edited August 8, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/290346-how-to-insert-calculate-values-to-database/#findComment-1487173 Share on other sites More sharing options...
Tje Posted August 8, 2014 Author Share Posted August 8, 2014 I'm new to PHP.can someone fix this for me??please Quote Link to comment https://forums.phpfreaks.com/topic/290346-how-to-insert-calculate-values-to-database/#findComment-1487174 Share on other sites More sharing options...
mac_gyver Posted August 8, 2014 Share Posted August 8, 2014 of the list of things that have been mentioned, which one(s) are you stuck on when you tried to do them? both of our replies mentioned calling your cal class? do you have code that does that? the code you posted only contains the definition of your cal class. Quote Link to comment https://forums.phpfreaks.com/topic/290346-how-to-insert-calculate-values-to-database/#findComment-1487191 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.