Jump to content


Photo

Adding submitted and current values together


  • Please log in to reply
2 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 07 March 2006 - 11:04 PM

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?

<?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...";
}
?>

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

#2 dcro2

dcro2
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts

Posted 07 March 2006 - 11:50 PM

[!--quoteo(post=352665:date=Mar 7 2006, 06:04 PM:name=master82)--][div class=\'quotetop\']QUOTE(master82 @ Mar 7 2006, 06:04 PM) View Post[/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:
$sql = mysql_query("SELECT * FROM Users");
$row = mysql_fetch_assoc($sql);

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


#3 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 08 March 2006 - 12:04 AM

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) View Post[/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:
$sql = mysql_query("SELECT * FROM Users");
$row = mysql_fetch_assoc($sql);

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users