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.

Link to comment
Share on other sites

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."'");

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.