Subtract From and Update mysql database field via php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ayodeleayobami
Forum Newbie
Posts: 2
Joined: Mon Dec 20, 2010 12:08 am

Subtract From and Update mysql database field via php

Post by ayodeleayobami »

Hello Peeps,
I am trying to do the following
1. receive input via a form
2. From the submitted variables, check the email in the database table and then pick the number field i want to subtract from
3. subtract the value from the submitted form from the value in the amount field in the database (I want to verify that the submitted value is less than the value in the database field)
4. Update the database with the value of the subtraction...
5. redirect to another page

I have written a script but i'm not sure if it will work.. I need experienced php programmers to please take a look and modify if necessary.. Thank you for your time and kind consideration..
The script is below

Code: Select all

<?

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name


//Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

// value sent from form
$Email=$_POST['Email'];
$AmtTransferred=$_POST['AmtTransferred']

// table name
$tbl_name=client;

// retrieve password from table where e-mail = $Email(mark@markydoe.com)
$sql="SELECT account_balance FROM $tbl_name WHERE email='$Email'";
$result=mysql_query($sql);
$val=(int)$_POST['AmtTransferred']
mysql_query("UPDATE client SET account_balance = '$result - $val'
WHERE email='$Email'");

header("Location: Processingpage1.htm");








?>
Last edited by Benjamin on Wed Dec 22, 2010 5:43 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Subtract From and Update mysql database field via php

Post by social_experiment »

ayodeleayobami wrote:I have written a script but i'm not sure if it will work..
What happens when you execute the code? It's usually a good idea to run the code prior to posting then you can also relate any problems / errors that you might have encountered.

Code: Select all

<?php mysql_query("UPDATE client SET account_balance = '$result - $val' WHERE email='$Email'"); ?>
This will probably not work because $result in this case will not yield a integer value. From the script it looks like you want to subtract the value from the form ($_POST['AmtTransferred']) from the value in the database (account_balance). You have to get the value from $result, using something akin to mysql_fetch_array(), mysql_fetch_row() or mysql_fetch_object().

Code: Select all

<?php
 while ($array = mysql_fetch_array($result)) {
 // equate the value to a variable to use outside
 // this while loop
 $acc_balance = $array['account_balance'];
 }

 $remainder = $acc_balance - $val;

 $update_query = mysql_query("UPDATE client SET account_balance = '". mysql_real_escape_string($remainder) ."' 
 WHERE email = '". mysql_real_escape_string($email) ."' ");

 if ($update_query) {
 header("location: Processingpage.htm");
 }
 else {
 // notify user / whoever of an error 
 }
?>
$val=(int)$_POST['AmtTransferred'] : Use is_numeric() to check values from a form because form input is always string.
PHP Manual wrote:Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ayodeleayobami
Forum Newbie
Posts: 2
Joined: Mon Dec 20, 2010 12:08 am

Re: Subtract From and Update mysql database field via php

Post by ayodeleayobami »

Thank you...
Took your advice and tried this version of the script first

Code: Select all

<?

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name


//Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

// value sent from form
$AccName=$_POST['AccName'];
$BrwAccNo=$_POST['BrwAccNo'];
$Email=$_POST['Email'];
$AmtTransferred=$_POST['AmtTransferred']
$BAName=$_POST['BAName'];
$BName=$_POST['BName'];
$RName=$_POST['RName'];
$Swt=$_POST['Swt'];
$BccNumber=$_POST['BccNumber'];
$BLocation=$_POST['BLocation'];

// table name
$tbl_name=client;

// retrieve password from table where e-mail = $Email(mark@phpeasystep.com)
$sql="SELECT account_balance FROM $tbl_name WHERE email='$Email'";
$query=mysql_query($sql);
$result=mysql_fetch_row($query);
$val=$_POST['AmtTransferred'];
$new_val=$result-$val;
mysql_query("UPDATE client SET account_balance = '$new_val' WHERE email='$Email'");

header("Location: Processingpage1.htm");

?>
When i execute this, nothing happens.. No errors and no subtraction either!! Can you please modify this and make it work so i can test?
Thank you in advance
Last edited by Benjamin on Wed Dec 22, 2010 5:43 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Subtract From and Update mysql database field via php

Post by social_experiment »

Code: Select all

<?php
 $result=mysql_fetch_row($query);
 $val=$_POST['AmtTransferred'];
 $new_val=$result-$val;
?>
You aren't accessing the resultset returned in $result = mysql_fetch_row($query).

Code: Select all

<?php
  while ( $acRow = mysql_fetch_row($query)) {
   foreach ($acRow as $field) {
     // access the value of 'account_balance' and assign
     // it to a variable.
     $account_balance = $field;
   }
 }
 //
 $val = $_POST['AmtTransferred'];
 $new_val = $account_balance - $val;
?>
There are also some security concerns but fix this part first then move onto that.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply