I don't know php but I need to fix a script. It adds a new row into the transactions table instead of checking for previous existence and updating. So would this do just that, check if the row exists and if so do some updates? I mostly needs syntax checking because I figure I got the logic side of it down.
$steamid would be "STEAM_0:1:12345"
$amount would be 1
[code]
$exists = mysql_query("SELECT steamid FROM transactions WHERE steamid='$steamid'");
if ($exists == $steamid) {
$prevamount = mysql_query("SELECT amount FROM transactions WHERE steamid='$steamid'");
$newamount = $prevamount + $amount;
mysql_query("UPDATE transactions SET amount='$newamount' WHERE steamid='$steamid'");
} else {
$newamount = $amount;
mysql_query("INSERT INTO transactions(steamid, transactionid, email, fname, lname, amount, status) VALUES('$steamid','$transactionid','$payeremail','$fname','$lname','$amount','$paymentstatus')");
}
[/code]
No... You need to use mysql_fetch_array or mysql_fetch_row to get the result.
EDIT: This should solve your problem.
[PHP]<?php
$query = mysql_query("SELECT steamid FROM transactions WHERE steamid='$steamid'");
$row = mysql_fetch_array($query);
$exists = $row['steamid'];
if ($exists == $steamid) {
$query = mysql_query("SELECT amount FROM transactions WHERE steamid='$steamid'");
$row = mysql_fetch_array($query);
$prevamount = $row['amount'];
$newamount = $prevamount + $amount;
mysql_query("UPDATE transactions SET amount='$newamount' WHERE steamid='$steamid'");
} else {
$newamount = $amount;
mysql_query("INSERT INTO transactions(steamid, transactionid, email, fname, lname, amount, status) VALUES('$steamid','$transactionid','$payeremail','$fname','$lname','$amount','$paymentstatus')");
}
?>[/PHP]
Also, a transactions table shouldn't be updated. It's used by my PayPal IPN script I made for you to make sure the Transaction ID haven't been processed before.
[code]
$newid = mysql_query("SELECT steamid FROM transactions WHERE steamid='$steamid'");
$row = mysql_fetch_array($newid);
$newid = $row['steamid'];
if ($newid == $steamid) {
$prevamount = mysql_query("SELECT amount FROM transactions WHERE steamid='$steamid'");
$row = mysql_fetch_array($prevamount);
$prevamount = $row['amount'];
$newamount = $prevamount + $amount;
mysql_query("UPDATE transactions SET amount='$newamount' WHERE steamid='$steamid'");
} else {
$newamount = $amount;
mysql_query("INSERT INTO transactions(*Cut down for post space*)");
}
[/code]
Oh, alright. Well consider the row doesn't exist in the first place, what would $newid, $row, $newid(the third line) produce as its variable?
If this was lua, the third line would error out if it tried preforming an action on a nil value. Does the same case apply or is it different with php?
You would get a "FALSE" response from MySQL. You can add this small code found on [URL="http://php.net"]http://php.net[/URL]:
[PHP]$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}[/PHP]
EDIT: If you'd only run the mysql_query without the mysql_fetch_array, the assigned variable would get a response similar to "Resource ID #8" or something along that string.
[QUOTE=Svenskunganka;38559455]No... You need to use mysql_fetch_array or mysql_fetch_row to get the result.
EDIT: This should solve your problem.
[PHP]<?php
$query = mysql_query("SELECT steamid FROM transactions WHERE steamid='$steamid'");
$row = mysql_fetch_array($query);
$exists = $row['steamid'];
if ($exists == $steamid) {
$query = mysql_query("SELECT amount FROM transactions WHERE steamid='$steamid'");
$row = mysql_fetch_array($query);
$prevamount = $row['amount'];
$newamount = $prevamount + $amount;
mysql_query("UPDATE transactions SET amount='$newamount' WHERE steamid='$steamid'");
} else {
$newamount = $amount;
mysql_query("INSERT INTO transactions(steamid, transactionid, email, fname, lname, amount, status) VALUES('$steamid','$transactionid','$payeremail','$fname','$lname','$amount','$paymentstatus')");
}
?>[/PHP]
Also, a transactions table shouldn't be updated. It's used by my PayPal IPN script I made for you to make sure the Transaction ID haven't been processed before.[/QUOTE]
Thanks, and I have all this within that code so it shouldn't be a problem. It does the whole transaction ID check before it gets to this new stuff.
[PHP]
if ($paymentstatus == "Completed" and $transactionid != $pretxnid and $receiveremail == "***" and $currency == "USD") {
$money = mysql_query("SELECT money FROM player_info WHERE steamid='$steamid'");
$row = mysql_fetch_array($money);
$money = $row['money'];
$ingamecash = $money + (4000*$amount);
mysql_query("UPDATE player_info SET money='$ingamecash' WHERE steamid='$steamid'");
$newid = mysql_query("SELECT steamid FROM transactions WHERE steamid='$steamid'");
$row = mysql_fetch_array($newid);
$newid = $row['steamid'];
if ($newid == $steamid) {
$prevamount = mysql_query("SELECT amount FROM transactions WHERE steamid='$steamid'");
$row = mysql_fetch_array($prevamount);
$prevamount = $row['amount'];
$newamount = $prevamount + $amount;
mysql_query("UPDATE transactions SET amount='$newamount' WHERE steamid='$steamid'");
} else {
$newamount = $amount;
mysql_query("INSERT INTO transactions(steamid, transactionid, email, fname, lname, amount, status) VALUES('$steamid','$transactionid','$payeremail','$fname','$lname','$amount','$paymentstatus')");
}
if ($newamount >= 15) {
mysql_query("UPDATE player_info SET rank='1' WHERE steamid='$steamid'");
}
}
[/PHP]
Should work fine.
EDIT: You should convert everything to MySQLi though.
[QUOTE=Svenskunganka;38559630]Should work fine.
EDIT: You should convert everything to MySQLi though.[/QUOTE]
Added to the list of to do, for now I'm just glad it works properly.
Sorry, you need to Log In to post a reply to this thread.