Jump to content

Adding a vaule to an existing value in a sql table


TheStalker

Recommended Posts

Sorry - I dont understand..

 

Do you mean you have a value stored in a table for example

 

Value

20

 

and want to be able to increment this value or make and adition to this value in the table..

 

If so - i think the way I would do it would be to pull the current value and then add the change in PHP then update the field in mysql but you may be able to do this directly with something like

 

Update my_table SET value = value + addition

 

This is not tested though

 

HTH

A simple example.

 

<form method="post">
  <input type="text" name="row">
  <input type="submit" name="submit" value="add">
</form>
<?php

if (isset($_POST['submit'])) {

  // connect to db.

  $row = mysql_real_escape_string($_POST['row']);
  $sql = "UPDATE tbl SET fld = fld+1 WHERE id = $row";
  if (mysql_query($sql)) {
    if (mysql_affected_rows()) {
      echo "$row updated</br >";
    }
  }
}

?>

thanks for all the replys ill explain in more detail ( i prob should have from the start)

 

i have a table called accounts that has customerID, Surname, newspaper and AmountOwing

 

when i click a button i want to have the value of a newspaper added to AmountOwing e.g

 

 

if newspaper = "sun" +0.35 AmountOwing

 

else if newspaper ="Times"  +0.45 AmountOwing

 

etc..........

 

 

 

 

<?php

$result = mysql_query("SELECT * FROM accounts");

 

while($row = mysql_fetch_array($result))

 

 

 

$value = $row['AmountOwing'];

 

echo $value;

 

if (['newspaper']="Sun");

{

+0.35 $value;

}

else if (['newspaper']="Times");

{

+0.45 $value;

}

 

echo $value;

?>

Archived

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

×
×
  • 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.