Hi, I'm having some trouble. I am fetching a list of "training tickets" from a table and each has its own transaction id for ease of coding. Everything appears as it should (though I'm in the very basics of tweaking that) but when I hit 'reject' it is deleting the wrong item from the table... despite the transaction id, so I'm very confused. It seems to work okay if I go from the bottom of the list up, but if I hit 'reject' on the first item on the list, it deletes the bottom one on the list.

$result = @mysql_query("SELECT transaction, player_id, owner_id, horse_id, accepted FROM training_tickets WHERE player_id='$player_id' AND accepted='0'") 
or die("Cannot find pending requests! " . @mysql_error());
while($row = @mysql_fetch_array($result)):
$name = $row['game_name'];
$their_id = $row['owner_id'];
                $transaction = $row['transaction'];
                $horse_id = $row['horse_id'];
echo "<tr valign=top>
<form action='trainingtickets.php' method=post>
<input type=hidden name='transaction' value='$transaction'>
<td class='spage'><center><input type=submit name='accept' value='Accept!'></td>
<td class='spage'>For horse: <a href='horses.php?id=$horse_id'>$horsename (#$horse_id)</a></td>
<td class='spage'>Owned by: <a href='home.php?id=$their_id'>$name (#$their_id)</a> - $afford</td>
<td class='spage'><center><input type=submit name='reject' value='Reject!'></td></tr></form>";
endwhile;
$pending_flag = 1;

    if(!$pending_flag){echo "<tr><td class='spage'>No pending training ticket requests.</td></tr>";}
    echo "</table>";
if($_POST['reject']){
@mysql_query("DELETE FROM training_tickets WHERE transaction='$transaction' LIMIT 1")or die("Cannot delete pending training ticket request! " . @mysql_error());
if(@mysql_affected_rows()){
myConfirm("Training request denied.");
}else{myError("Invalid training request.");}

Are there any obvious errors? I have tried just about everything I could think of with no luck. I even had the transaction id appear in the form (i.e. it would post to trainingtickets.php?id=$transaction) and the transaction number would be correct for what I clicked, but it deleted something different from the table. Thanks in advance for help.

Recommended Answers

All 3 Replies

I would recommend you turn off all your error suppressions (@) and see if you raise any errors...

Line 1 is missing a closing ;

While I can't say it's improper, I have never seen a while loop in PHP using

while($mycontingency):
...
endwhile;

I would encourage you to use

while($mycontingency) {
...
}

EDIT: I just looked on the PHP Manual and while($foo): is totally acceptable, just not very PHP-ish as few statements follow this design. You can happily ignore the above.

line 21 you are basically saying suppress all errors but if there is an error print the error but supress the error. Not sure you are using error suppression correctly...

As to your problem...

I have had problems where queries do not run properly if they are not closed with a semi-colon.

So, try changing it to
mysql_query("DELETE FROM training_tickets WHERE transaction='$transaction' LIMIT 1;") or die ("Could not delete: ".mysql_error());

Lastly, for the sake of "true" RBDM, you should never delete any items from your database. Instead, you should add another column called "deleted" and make it a datetime, and when you run your selects you should check that datetime <= 0, and when you want to delete the item you should set datetime to "now()"

Okay, I took all of your advice and it still didn't work. No matter what I do, it's deleting - or now, in this case, just setting accepted=3 to the ones I reject, from the bottom up... even if I'm clicking the reject button on the 1st or 2nd.

I figured it out. I used GET to get the transaction id under the post accept and post reject. Works like a charm now. Whew. Thanks for all your help and suggestions, though! I modeled it after programming that was done for me, so I'm not quite sure I understand error suppression but I will certainly look into it. Thanks again, take care.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.