16, October 2008

Could anyone tell me how to...? - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Could anyone tell me how to...?
« previous next »
Pages: [1] 2 Print

Author Topic: Could anyone tell me how to...?  (Read 1442 times)
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« on: Jan 24, 2007, 01:36:10 AM »

Hi guys, first of all, a brief and as clear as possible as i could explanation of what i want to do:

This is for an Account Balance script. When a new customer signs up, the customer will have into a cell of my SQL his balance, let's say: 1,200$.
Obviously into the database, for every user row I have the Balance field with it's respective and particular balance.

Now here is where I get stuck.
Let's say that the customer has to pay 100$ per month in order to clear the balance when the 1 year (or anything) period it's over.

So, customer goes every month at the bank, customer sends his payment to our bank account, customer logs into our website and sends the payment confirmation with it's amount via a web form.
so, that form sends a $_GET parameter to another script that will UPDATE how much balance is left into the account in our database.

My plain question is: How to rest a particular amount to a MySQL query, and then update the amount in the database?

Regards everyone!
« Last Edit: Jan 24, 2007, 01:38:56 AM by eliezer »
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Jan 24, 2007, 09:54:30 AM »

Is this problem that kind of simple or do I not understand?

It sounds like a normal update statement to me:

Code:
$sql = sprintf('UPDATE table SET amount = %f', $_GET['conf_amount'])


Last blog : Upload images for usage in TinyMCE
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #2 on: Jan 24, 2007, 11:15:06 AM »

So, if the 'amount' on 'table' is 1200 and i run that query with a $_GET['conf_amount']) value of 120, will that update the 'amount' to 1080?

p.s. could you explain me the %f thingie? to be honest, never heard of sprintf() and I do not understand the %f thing. I guess %f takes the value of $_GET['conf_amount']) to work it into the query. huh?
« Last Edit: Jan 24, 2007, 11:19:51 AM by eliezer »
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Jan 24, 2007, 11:28:26 AM »

check the manual for sprintf

to get the the right amount you need to calculate the value:

$sql = sprintf('UPDATE table SET amount = amount - %f', $_GET['conf_amount'])



Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8133
41755 credits
Members referred : 3



« Reply #4 on: Jan 24, 2007, 01:19:07 PM »

Of course sprintf is not necessary. You can replace this :

sprintf('UPDATE table SET amount = amount - %f'$_GET['conf_amount'])

with this

'UPDATE table SET amount = amount - '.(int)$_GET['conf_amount']

The (int) is to make sure that none is going to play around with your database (SQL injection attacks)

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Jan 24, 2007, 01:26:20 PM »

Of course sprintf is not necessary. You can replace this :

sprintf('UPDATE table SET amount = amount - %f'$_GET['conf_amount'])

with this

'UPDATE table SET amount = amount - '.(int)$_GET['conf_amount']

The (int) is to make sure that none is going to play around with your database (SQL injection attacks)

Nick %f is a float conversion...


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8133
41755 credits
Members referred : 3



« Reply #6 on: Jan 24, 2007, 01:35:10 PM »

I am not sure what eliezer meant Smiley

Anyway if the $_GET value is integer, you don't need sprintf

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #7 on: Jan 24, 2007, 03:47:45 PM »

Thanks once again for the support guys.
I see what Nikolas is telling me, it could work.
But, just to make sure, if I use Olaf's example I would be able to handle cents, right?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Jan 24, 2007, 03:58:02 PM »

Thanks once again for the support guys.
I see what Nikolas is telling me, it could work.
But, just to make sure, if I use Olaf's example I would be able to handle cents, right?
yes, if your database table column can handle it


Last blog : Upload images for usage in TinyMCE
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #9 on: Jan 25, 2007, 04:31:38 AM »

Thanks once again for the support guys.
I see what Nikolas is telling me, it could work.
But, just to make sure, if I use Olaf's example I would be able to handle cents, right?
yes, if your database table column can handle it

varchar Shall support it, shouldn't it? Because it has no Integral (sp Integer?) value, right?
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #10 on: Jan 25, 2007, 05:07:53 AM »

Would this be right? specially the mysql_query line.
Sorry, Im a noob.  Embarrassed
Im going to try the code and try to find the error wich Im sure will be there. But anyway, if you got any advise my ears are wide open.





<html>
<head>
<title> </title>
<link href="styles.css" type="text/css" rel="stylesheet">
<script language="JavaScript" type="text/javascript" src="rte/wysiwyg.js"></script>
</head>
<body>
<BR>
<P>
<?php
$monto=$_GET["monto"];
$cliente=$_GET["cliente"];
$self = $_SERVER['PHP_SELF'];
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("lagunadesign", $con);


if($monto)
  {
/*
*   actualizamos los datos si recibimos un pago.
*/
   echo "<P><BIG>Confirm payment amount: <font color=red>" . $fecha . "</font>.</BIG></P>";
   $operacion = mysql_query("sprintf('UPDATE clientes WHERE id_cliente = '" . $cliente . "' SET saldo = saldo - %f',

$_GET['monto'])");
   echo "<P>Pago recibido por un monto de: ";
   echo $monto;
   echo " pesos.</P>";
  }

mysql_close($con);

$carga = microtime();
print '<P>Load time: ';
print (number_format($carga,2));
print ' seconds.</P>';
 
?>
</body>
</html>
« Last Edit: Jan 25, 2007, 05:14:24 AM by eliezer »
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #11 on: Jan 25, 2007, 06:55:25 AM »

please edit your
Thanks once again for the support guys.
I see what Nikolas is telling me, it could work.
But, just to make sure, if I use Olaf's example I would be able to handle cents, right?
yes, if your database table column can handle it

varchar Shall support it, shouldn't it? Because it has no Integral (sp Integer?) value, right?

varchar can simply hold a decimal number such as "22.56", but can not use it for calculations after it is stored in the database unless you modify the datatype to handle decimals.. otherwise you could use varchar, if you simply want to store that number and update it from time to time

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #12 on: Jan 25, 2007, 09:10:29 AM »

Quote
varchar Shall support it, shouldn't it? Because it has no Integral (sp Integer?) value, right?

varchar is a text type, of course you can canvert each string to a integer/float, but for better performance you should use a decimal or integer column type.

btw. in your last code snipped, where is the sql statement?


Last blog : Upload images for usage in TinyMCE
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #13 on: Jan 25, 2007, 11:46:02 AM »

Hey guys, I got a little bit confused with the sprintf thing. I cannot get it to work so instead I chose Nicolas option and it did work BUT when I add a WHERE statetement to the query, it quits working. If I remove the WHERE statement it updates the whole CUSTOMERS table.
Here's my code:

<html>
<head>
<title> </title>
<link href="styles.css" type="text/css" rel="stylesheet">
<script language="JavaScript" type="text/javascript" src="rte/wysiwyg.js"></script>
</head>
<body>
<BR>
<P>
<?php
$amount=$_GET["amount"];
$id_customer=$_GET["id_customer"];
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("lagunadesign", $con);

if($amount)
{
$sql='UPDATE customers SET balance = balance - ' . (int)$_GET['amount'] . ' WHERE id_customer=" . $id_customer . "';
$result = mysql_query($sql);
echo "<P>Domain name <font color=red>" . $_GET[id_customer] . "</font> registered succesfully a payment amount of <font color=red>" . $_GET[amount] . "</font> pesos.</P>";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
}

mysql_close($con);
?>
</P>
</body>
</html>
« Last Edit: Jan 25, 2007, 11:49:26 AM by eliezer »
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #14 on: Jan 25, 2007, 12:23:05 PM »

replace this and tell us the error:

$result = mysql_query($sql) or die(mysql_error());


Last blog : Upload images for usage in TinyMCE
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #15 on: Jan 25, 2007, 12:31:01 PM »

Replaced it, nothing happenned. Not even error message.
Here's the script in action, right now it's exactly the same code as above with the lined you asked to be replaced, replaced.

http://189.153.101.203/lagunadesign/recibir_pago.php?cliente=eliezer.com&monto=127 Visit through proxy
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #16 on: Jan 25, 2007, 12:44:46 PM »

Quote
I chose Nicolas option and it did work BUT when I add a WHERE statetement to the query, it quits working.

what happens then?


Last blog : Upload images for usage in TinyMCE
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #17 on: Jan 25, 2007, 03:37:21 PM »

It just doesn't change and store the new amount.
It shows the succesfull message inside the loop ( {...} )  but the update through the query it just doesn't seem to work even though it seems to be properly stated.  Undecided
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #18 on: Jan 25, 2007, 03:41:44 PM »

try
Code:
echo "Customer id: $id_customer";
just before the update statement. That should show you the customer ID on screen so you can verify that it is filled correctly


Last blog : Are You Stumbling Yet?
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #19 on: Jan 25, 2007, 03:51:13 PM »

It didn't show anything. mhmmm...
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=5738
Tags : math mysql operations balance account Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] 2 Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Could anyone tell me how to...?
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Oct 16, 2008, 10:28:09 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!





Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.