Jump to content

Recommended Posts

Hello, (first post so hopefully someone will help me)

I've got a MYSQL database with the table 'Users'
Within this is the field 'Sales'

I have created a simple HTML form that allows me to input a number and submit it (simple stuff!) (and told it to run salesupdate.php once submitted)

NOW, im stuck trying to create the 'salesupdate.php' that will do the following:

1. Retrieve the submitted number from the HTML form ($_POST['sales'] if im correct?) [eg 10]
2. Retrieve the number already contained within the database [eg 25]

THEN (if possible)
3a. multiply the submitted number by 0.75 [eg 10*0.75 = 7.5] and then add this to the number in the database [eg 7.5 + 25 = 32.5 in the field]

OR (if not possible above)
3b. Simple take the value from the submitted and the database and add them together, replacing the old database number.

Thanks in advance
Link to comment
https://forums.phpfreaks.com/topic/4361-updating-a-single-field-in-mysql/
Share on other sites


You could:

[code]$sql = mysql_query("SELECT * FROM Users WHERE user_id = '$userid'"); //I'm just guessing about the user_id.
$row = mysql_fetch_assoc($sql);

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

$sql = mysql_query("UPDATE Users SET num_field = '$finalnumber'");
if($sql) {
  echo "Success!";
} else {
  echo "Failed...";
}[/code]
Of course, replace 'num_field' with the field in your database.
Thanks for that speedy reply :)
I added the essentials such as PHP tags and connection string and tested. It ran and produced the success! message, however it replaced the value in the table to 0.

Could someone please look at the code I tried - I think there is a problem with lines 8 & 9 as they are both $finalnumber. (can I change the 2nd to $finalnum, and the one on line 11 too? - think i'll test that, also go back to my HTML form see if an error lies there).

Anyone spot anything wrong here?

[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);
$finalnumber = $finalnumber + intval($row['SalestoDate']);

$sql = mysql_query("UPDATE Users SET SalestoDate = '$finalnumber'");
if($sql) {
  echo "Success!";
} else {
  echo "Failed...";
}
?>
[/code]
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.