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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.