Jump to content

Archived

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

master82

Adding submitted and current values together

Recommended Posts

OK, below is my current code. It takes a value from a submitted form and adds this value to my 'users' table in the 'salestodate' field.

I was hoping that it would also add this number to the current number stored on the database, but it replaces it (with the submitted number) rather than replace it withthe sum of the two (submitted and that already on the database).

Anyone know what I can tweek to make this happen?

[code]
<?php

$conn = mysql_connect("localhost", "username", "password");
mysql_select_db("database",$conn);

$sql = mysql_query("SELECT * FROM Users");

$finalnumber = intval($_POST['sales']) * (0.75);
$finalnum = ($finalnumber + intval($row['salestodate']));

$sql = mysql_query("UPDATE Users SET salestodate = '$finalnum'");
if($sql) {
  echo "Success!";
} else {
  echo "Failed...";
}
?>
[/code]

Thanks in advance to the genious who has the solution :)

Share this post


Link to post
Share on other sites
[!--quoteo(post=352665:date=Mar 7 2006, 06:04 PM:name=master82)--][div class=\'quotetop\']QUOTE(master82 @ Mar 7 2006, 06:04 PM) [snapback]352665[/snapback][/div][div class=\'quotemain\'][!--quotec--]$sql = mysql_query("SELECT * FROM Users");

$finalnumber = intval($_POST['sales']) * (0.75);
$finalnum = ($finalnumber + intval($row['salestodate']));
[/quote]

In this line:
$finalnum = ($finalnumber + intval($row['salestodate']));
you haven't fetched the results from:
$sql = mysql_query("SELECT * FROM Users");

You need to use:
[code]$sql = mysql_query("SELECT * FROM Users");
$row = mysql_fetch_assoc($sql);

$finalnumber = intval($_POST['sales']) * (0.75);
$finalnum = ($finalnumber + intval($row['salestodate']));[/code]

Share this post


Link to post
Share on other sites
Cheers for the reply,

I've added the line that you suggested but still no luck, its still only populating the field with the submitted number and not a combination of the two.

Is there any other approach I could use?

[!--quoteo(post=352678:date=Mar 7 2006, 11:50 PM:name=dcro2)--][div class=\'quotetop\']QUOTE(dcro2 @ Mar 7 2006, 11:50 PM) [snapback]352678[/snapback][/div][div class=\'quotemain\'][!--quotec--]
In this line:
$finalnum = ($finalnumber + intval($row['salestodate']));
you haven't fetched the results from:
$sql = mysql_query("SELECT * FROM Users");

You need to use:
[code]$sql = mysql_query("SELECT * FROM Users");
$row = mysql_fetch_assoc($sql);

$finalnumber = intval($_POST['sales']) * (0.75);
$finalnum = ($finalnumber + intval($row['salestodate']));[/code]
[/quote]

Share this post


Link to post
Share on other sites

×

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.