Orasion Posted August 5, 2011 Share Posted August 5, 2011 Hi all, Im currently trying to code a subtraction from database as in example I have database field "Total" with some integer. I want to substract the value with my input from post. Err, If you dont quite get what Im trying, I try to do this update cycle set total=total-1 where id=1; but with php How could I achieve it? :confused: thx in advance Quote Link to comment https://forums.phpfreaks.com/topic/243942-ask-simple-subtraction-from-database/ Share on other sites More sharing options...
Pikachu2000 Posted August 5, 2011 Share Posted August 5, 2011 Are you talking about doing something like this? $integer = (int) $_POST['form_field']; $query = "UPDATE cycle SET total = (total - $integer) WHERE id = 1"; // etc. Quote Link to comment https://forums.phpfreaks.com/topic/243942-ask-simple-subtraction-from-database/#findComment-1252573 Share on other sites More sharing options...
Maq Posted August 5, 2011 Share Posted August 5, 2011 If I understand you correctly, when you receive the POST value, just put it in your query string. $subValue = mysql_real_escape_string($_POST['subValue']); $sql = "update cycle set total=total-".$subValue." where id=1;"; $query = mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/243942-ask-simple-subtraction-from-database/#findComment-1252575 Share on other sites More sharing options...
Orasion Posted August 5, 2011 Author Share Posted August 5, 2011 okay, honestly Im doing it in codeigniter but I think its just the same. In controller I write this $data = array( 'total' => (total-$this->input->post('total')), ); $this->db->where('id', $this->input->post('id')); //$this->input->post('id')=1 $this->db->update('cycle', $data); And that should output sql query like this (CMIIW) update cycle set total=total-1 where id=1 As for the result, before the subtraction the data is "9" but after the subtraction I have "-1" So, what did I do wrong? Quote Link to comment https://forums.phpfreaks.com/topic/243942-ask-simple-subtraction-from-database/#findComment-1252603 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.