0

I am not-so-gently pulling my hair out over here.

I created a simple MySQL update using PHP that changes a flag in the db from '1' to '2' if it was previously set to '1', or from a '2' to '1' if it was previously set to '2'. Nothing special there.

However, when I click the link, the db is updating accordingly but the text on the site is doing something very funky. I have attached a video to show what I mean: http://gfycat.com/RipeCarefulChinesecrocodilelizard

At the beginning of the video, you will see what's happening as I click quickly. Closer to the middle of the video, I click a link and then refresh the page. Refreshing shows the correct flagging for each of the items.

I have never experienced this before. I am not sure if there is some weird caching issue happening or what. That said, this is super simple HTML/PHP markup grabbing variables using GET and shouldn't be screwing up like this.

Any ideas what might be going on? (Better yet, what a fix could be?)

The code is as follows:

<?php
// Make a MySQL Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("selected_db") or die(mysql_error());


// if the vendor's commission is being changed
$vendor_id = $_GET['id'];
$switch_type = $_GET['type'];

if ($switch_type == "reg") {
    $result2 = mysql_query("UPDATE vendors SET vendor_commission_rate='1' WHERE vendor_id='$vendor_id'") 
        or die(mysql_error());
}
elseif ($switch_type == "pro") {
    $result2 = mysql_query("UPDATE vendors SET vendor_commission_rate='2' WHERE vendor_id='$vendor_id'") 
        or die(mysql_error());
}
?>
<style>
table, tr, td, th {
    padding:5px;
}
</style>
<center>
<table>
  <tr>
    <td colspan="5" style="padding-bottom:20px; text-align:center; font-size:130%">Item sold for <strong>$20.00</strong></td></tr>
    <th valign="bottom">Vendor</th><th align="center" valign="bottom">Commission<br />Rate</th><th align="center" valign="bottom">Vendor<br />Commission</th><th align="center" valign="bottom">supplier<br />Commission</th><th></th></tr>
<?php
$result = mysql_query("SELECT * FROM vendors") 
    or die(mysql_error()); 

    while ($row = mysql_fetch_array($result)) {
        $vendor_id = $row['vendor_id'];
        $vendor_name = $row['vendor_name'];
        $rate_amount = $row['vendor_commission_rate'];

            if ($rate_amount == 1) { 
                $rate_amount = 0.5;
                $rate_name = "<a href=\"?id=".$row['vendor_id']."&type=pro\">Upgrade to Pro</a>";
            }
            if ($rate_amount == 2) { 
                $rate_amount = 0.75;
                $rate_name = "<a href=\"?id=".$row['vendor_id']."&type=reg\">Switch to Reg</a>";
            }

            $vendor_commission = 20 * $rate_amount;
            $supplier_commission = 20 - $vendor_commission;
            $rate_amount_output = $rate_amount * 100;

        echo "<tr><td>".$vendor_name."</td><td align=\"center\">".$rate_amount_output."%</td><td align=\"center\">$".number_format($vendor_commission,2)."</td><td align=\"center\">$".number_format($supplier_commission,2)."</td><td>".$rate_name."</td></tr>";
    }
?>
</table>
</center>

Thanks for your help!

Sheldon Scott
  • 741
  • 1
  • 7
  • 21
  • There is no JavaScript involved (can't really tell from that gif)? – Siguza Aug 15 '15 at 18:02
  • No JavaScript at all at this point. – Sheldon Scott Aug 15 '15 at 18:03
  • Also, [you shouldn't be using `mysql_` anymore](https://stackoverflow.com/q/12859942) and your code is [vulnerable to SQL injections](https://stackoverflow.com/q/60174). – Siguza Aug 15 '15 at 18:06
  • Agreed. This is just a simple hack concept build. :) – Sheldon Scott Aug 15 '15 at 18:07
  • Looks like you are fetching `vendor_commission_rate` from `table_name2`, but are updating it in `table_name`, could that be it? – Siguza Aug 15 '15 at 18:17
  • Let me give that a shot. – Sheldon Scott Aug 15 '15 at 18:20
  • Updated the code in the OP. Still having the same issue. – Sheldon Scott Aug 15 '15 at 18:34
  • Well, I had to [rewrite it to use PDO](http://pastebin.com/1Q6xAHmC) in order to test it on my PHP installation, and it works fine here ([DB dump](http://pastebin.com/jFqraQEa)). Are you able to reproduce it with PDO? If not, I suggest you just move on. – Siguza Aug 15 '15 at 19:28
  • Works fine for me in PDO as well, until I have clicked a link multiple times. It seems as though when it loops around to trying the update on an item the third (or so) time, that's when the problem arises. – Sheldon Scott Aug 15 '15 at 19:50

0 Answers0