Jump to content

Archived

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

master82

Updating a single field in MYSQL

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
FIXED!!! was a problem with the HTML form I created and the two $finalnumber.

Thanks for your help :)

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.