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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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