Jump to content


Photo

Updating a single field in MYSQL


  • Please log in to reply
3 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 07 March 2006 - 08:54 PM

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

#2 dcro2

dcro2
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts

Posted 07 March 2006 - 09:08 PM


You could:

$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...";
}
Of course, replace 'num_field' with the field in your database.

#3 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 07 March 2006 - 10:12 PM

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?

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


#4 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 07 March 2006 - 10:29 PM

FIXED!!! was a problem with the HTML form I created and the two $finalnumber.

Thanks for your help :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users