Jump to content

[ask] simple subtraction from database


Orasion

Recommended Posts

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: :confused:

 

thx in advance

Link to comment
https://forums.phpfreaks.com/topic/243942-ask-simple-subtraction-from-database/
Share on other sites

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);

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.