Jump to content

[SOLVED] How do I address a single value in a single table using mySQL and php


ahvceo

Recommended Posts

Hi All,

 

20 years ago I thought I knew how to do a query but I guess I have forgotten.  I am trying to update a single value in one row in one table in a database with 5 tables.  I thought the following code would work but no joy.

 

$payment = "3.50";

$ref = $_COOKIE['ref'];

mysql_connect($server, $db_user, $db_pass)
      or die ("Database CONNECT Error (line "); 

$monthlydue = mysql_db_query($database, "select monthlydue from affiliates where memid=" . $ref);

$monthlydue = $monthlydue + $payment

mysql_db_query($database, "INSERT INTO affiliates (monthlydue) VALUES ('".$monthlydue."') where memid =" . $ref) 
        or die(mysql_error());

 

Thyere are no errors in the database values or names because I use the same values and names to add rows with no problem.  If it makes any difference the value I am trying to return is a double but I have the same problem with a string when I try to access just the email address.  Can anyone tell me what I am doing wrong?

 

Thanks

ahvceo

 

PS Would you believe I have looked a over 20 examples of this on the net and not a single one of them addressed the issue of accessing only one value in a table.  Every one showed how to return or insert or add or update a complete row, but not one showed how to get or insert a single value in a row.

Code should be:

<?php

$payment = "3.50";

$ref = $_COOKIE['ref'];

mysql_connect($server, $db_user, $db_pass)
      or die ("Database CONNECT Error (line ");
      
      
mysql_select_db($database); //cant see the $database var defined anywhere....

$monthlydue = mysql_query("select `monthlydue` from `affiliates` where `memid`= '".$ref."'");
while	($row = mysql_fetch_array($monthlydue))
{
mysql_query("UPDATE `affiliates` SET `monthlydue` ='".$row['monthlydue'] + $payment."' WHERE `memid` ='".$ref."'");
}
?>

You may also be able to do this:

<?php

$payment = "3.50";

$ref = $_COOKIE['ref'];

mysql_connect($server, $db_user, $db_pass)
      or die ("Database CONNECT Error (line ");
      
      
mysql_select_db($database); //cant see the $database var defined anywhere....

mysql_query("UPDATE `affiliates` SET `monthlydue` = '`monthlydue`+".$payment."' WHERE `memid` ='".$ref."'");

?>

Hold up? The above code is the working bit to insert things into the database, but your having issues with the other end and getting items from it?

 

If so you might like to check out this tutorial, which covers all the basics: http://www.phpfreaks.com/tutorial/php-basic-database-handling

Hi

 

To the OP. You are mixing up the syntax for an insert and an update.

 

The 2nd suggestion from papaface is probably best. Doing the update there is no need to read it first (although you might want to if you are making use of the other columns).

 

<?php

$payment = "3.50";

$ref = $_COOKIE['ref'];

$conn = mysql_connect($server, $db_user, $db_pass) or die ("Database CONNECT Error (line ");
      
mysql_select_db($database, $conn); //cant see the $database var defined anywhere....

mysql_query("UPDATE `affiliates` SET `monthlydue` = `monthlydue`+ $payment  WHERE `memid` ='$ref'", $conn) or die(mysql_error());

?>

 

All the best

 

Keith

Hi All,

 

Thanks for the help!  I am really getting too old for this stuff.  What I used to do in 5 minutes now takes me an hour or two.  I was really starting to get frustrated but finally got there.  Your code works and now so does mine.

 

Thank you all!

ahvceo

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.