ayodeleayobami Posted November 26, 2010 Share Posted November 26, 2010 I don't know where to start with this so i guess i'll just start anywhere I have a database table that i am using to design an application from tutorials i have managed to snag from here and there... Now i can login, display information for a particular user etc I have a form with a form field say x and i want my users to be able to submit the form and deduct the variable from form field x from a field in my database table and then replace the result in the same field in the database so suppose my database field is balance, i need to submit the form and have it do the following 1. balance-x = new balance 2. save new balance in database field balance Help will be much appreciated.. Thank you Link to comment https://forums.phpfreaks.com/topic/219944-using-form-fields-to-subtract-from-a-database-table/ Share on other sites More sharing options...
Pikachu2000 Posted November 26, 2010 Share Posted November 26, 2010 The math can be don in the query itself, and the new value saved. Assuming the database field is an integer, and the form field name is 'value', this should give enough to get started. $_POST['value'] = trim($_POST['value']; if( !empty($_POST['value']) && ctype_digit($_POST['value']) ) { $val = (int) $_POST['value']; $query = UPDATE `table_name` SET `field` = `field` - $val WHERE `user_field` = 'identifying_value'; if( $result = mysql_query($query) ) { echo "Number of records updated: " . mysql_affected_rows(); } } Link to comment https://forums.phpfreaks.com/topic/219944-using-form-fields-to-subtract-from-a-database-table/#findComment-1140072 Share on other sites More sharing options...
ayodeleayobami Posted December 8, 2010 Author Share Posted December 8, 2010 Thank you, This is a sample of the code.. Its does not seem to be working.. Please help ...I have two variables being sent from the form.. Email and Amount to Reduce... I am selecting the row containing the email and doing this deduction..I also want to be able to verify that a negative or zero sum is not entered or better still the amount entered in the form is nor larger than the amount in the Database. Here goes <? $host="localhost"; // Host name $username=""; // 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"); // valued sent from form $Email=$_POST['Email']; $Amount=$_POST['Amount']; // table name $tbl_name=client; // retrieve Amount from table where Email = $Email $sql="SELECT Amount FROM $tbl_name WHERE Email='$Email'"; $result=mysql_query($sql); // if found this e-mail address, row must be 1 row // keep value in variable name "$count" $count=mysql_num_rows($result); $_POST['Amount'] = trim($_POST['Amount']; if( !empty($_POST['Amount']) && ctype_digit$_POST['Amount']) ) { $val = (int) $_POST['Amount']; $query = UPDATE `client` SET `Amount` = `field` - $val WHERE `user_field` = 'identifying_value'; } header("Location: post.php"); ?> Link to comment https://forums.phpfreaks.com/topic/219944-using-form-fields-to-subtract-from-a-database-table/#findComment-1144221 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.