TTMW Posted August 19, 2007 Share Posted August 19, 2007 im geting variables from flash using php.but i dont know how to use them variables in these statement... $placeEarnt = $_POST['placeEarnt']; $earnings = mysql_query("SELECT '$placeEarnt' FROM users WHERE username = '$username'")or die(mysql_error()); if(mysql_num_rows($earnings)==0) echo "There is no data in the table"; else for($i=0;$i<mysql_num_rows($earnings);$i++) { $row=mysql_fetch_assoc($earnings); } $updateEarnings = $row[earnings] + $_POST['tokens']; $result = mysql_query("UPDATE users SET $placeEarnt ='$updateEarnings' WHERE username = '$username'") or die(mysql_error()); this doesnt throw an error but only adds the value in $_POST['tokens'] to the database.does anyone know how i can make this work?thanks Quote Link to comment https://forums.phpfreaks.com/topic/65702-using-php-variables-in-mysql-statment/ Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 its doing exactly what you coded it to do ? what do you want it to do ? Quote Link to comment https://forums.phpfreaks.com/topic/65702-using-php-variables-in-mysql-statment/#findComment-328143 Share on other sites More sharing options...
TTMW Posted August 20, 2007 Author Share Posted August 20, 2007 well at the moment when the code runs it just puts the value held by the 'tokens' variable into the earnings row.and doesn't add it to the current amount. Quote Link to comment https://forums.phpfreaks.com/topic/65702-using-php-variables-in-mysql-statment/#findComment-329156 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 to update i think you want this for($i=0;$i<mysql_num_rows($earnings);$i++) { $row=mysql_fetch_assoc($earnings); $updateEarnings = $updateEarnings + $row[earnings];//add up amount } $updateEarnings = $updateEarnings + $_POST['tokens']; $result = mysql_query("UPDATE users SET $placeEarnt ='$updateEarnings' WHERE username = '$username'") or die(mysql_error()); revised (try this also) $placeEarnt = $_POST['placeEarnt']; $earnings = mysql_query("SELECT SUM($placeEarnt) as TotalAmount FROM users WHERE username = '$username'")or die(mysql_error()); $row=mysql_fetch_assoc($earnings); $updateEarnings = $row['TotalAmount'] + $_POST['tokens']; $result = mysql_query("UPDATE users SET $placeEarnt ='$updateEarnings' WHERE username = '$username'") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/65702-using-php-variables-in-mysql-statment/#findComment-329269 Share on other sites More sharing options...
Hypnos Posted August 21, 2007 Share Posted August 21, 2007 Looks like you are using '$placeEarnt' as a field, then later in the code you're assuming that the fieldname is "earnings". Isn't it dynamic? <?php $placeEarnt = $_POST['placeEarnt']; //I added "AS earings" because your column is set by a var. $earnings = mysql_query("SELECT '$placeEarnt' AS earnings FROM users WHERE username = '$username'")or die(mysql_error()); if(mysql_num_rows($earnings)==0) echo "There is no data in the table"; else { //I assume you want the else for more than this empty line... Added a bracket here //Only need to do this once, if there is only going to be one row $row=mysql_fetch_assoc($earnings); $updateEarnings = $row['earnings'] + $_POST['tokens']; $result = mysql_query("UPDATE users SET $placeEarnt ='$updateEarnings' WHERE username = '$username'") or die(mysql_error()); } //..and here Quote Link to comment https://forums.phpfreaks.com/topic/65702-using-php-variables-in-mysql-statment/#findComment-329403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.