Jump to content

using php variables in mysql statment


TTMW

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/65702-using-php-variables-in-mysql-statment/
Share on other sites

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());

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.