ragebunny
ragebunny

Reputation: 1760

I can't update a record in my database

I have a system where i want to up date a record, change its status between 0 and 1. To turn a feature on or off. This is my form to turn it on or off:

<form id="form1" name="form1" method="post" action="">
            <label>
                Select Market: 
                    <select name="market" id="market">
                        <option value="EUR/USD">EUR/USD</option>
                        <option value="gbpusd">GBP/USD</option>
                        <option value="chfusd">CHF/USD</option>
                        <option value="brent">Brent</option>
                        <option value="GOLD">Gold</option>
                        <option value="downjones">Down Jones</option>      
                    </select>
            </label>
            <label>
                <input type="submit" name="on" id="on" value="On">
                <input type="submit" name="off" id="off" value="Off">
            </label>
        </form>

Now this is the php for updating the record:

if(isset($_POST['on']))
{
    if(isset($_POST['market']))
    {
        if($_POST['market'] == 'EUR/USD')
            $market_id = '1';

        $updateRecord = mysql_query("UPDATE current_trades SET status='1' WHERE id='$market_id'");

        //Update record by turning it on
        echo "market on";
    }
}

So as you can see, the user would select a value from the drop down menu and the hit on or off to turn that feature on or off. But when i click on (Which is the only one with logic so far) it doesn't update my record, any see anything wrong with that?

I don't think its anything to do with permissions because i have other pages editing other tables in my database.

also if i go into phpMyAdmin and run the Mysql from there it works.

I have also tested just grabbing some records from my database and this works just fine.

So any ideas?

Upvotes: 1

Views: 1602

Answers (1)

bfavaretto
bfavaretto

Reputation: 71918

The problem is $market_id is not defined (except when $_POST['market'] == 'EUR/USD'), so the updated query will fail (it will look for records WHERE id='', which is probably none).

Maybe you also want the mysql_query and echo lines inside the if? In this case, you need { and } around the whole block:

if($_POST['market'] == 'EUR/USD') {
    $market_id = '1';
    $updateRecord = mysql_query("UPDATE current_trades SET status='1' WHERE id='$market_id'");
    //Update record by turning it on
    echo "market on";
}

EDIT

I re-read your question, and now I think the code I suggested is not what you need. What you need is to have a proper $market_id set for each possible selected value from #market. The easiest solution would be to make the value attribute of each <option> the market id, like

<option value="1">EUR/USD</option>

Then you PHP you'd just do $market_id = is_numeric($_POST['market']) ? $_POST['market'] : 0;.

If you don't want that, you'll need a switch statement on your PHP, or a bunch of if/else. The important thing here is that you need a different $market_id set for each situation, so you can run the right query based on that.

Upvotes: 1

Related Questions