开发者

PHP Form Posting Values To Database

Basically, i have a working form where the user inputs details about their laptop to sell to my shop. I give them a quote once they have submitted the Specs of the laptop. At the moment i have got option boxes and checkboxes which each have a value-- for example these. ---

<label for="state">State</label><br>

<select name="state">

<option value="10">Excellent</option>

<option value="5">Good</option>

<option value="0">Poor</option>

</select><br>

The Values of the options they have selected get added up at the end and that gives them the quote - in the above example - "10" means £10 extra for a excellent condition laptop etc.

I use $_POST[state] to get the value of it to add onto the other op开发者_开发问答tions for the quote.

But my problem lies when i POST them to a database (so we can check when they come in). When they get added to the database, obviously it just comes out as the values not the actually name of it like "excellent" or "good". just says "10" or "5".

Is there anyway to put the name of the option into the database instead of the value?


sure... just make sure that's what you want to do. It's usually not considered a good database practice to create denormalized tables like that, but you could do it. When you collect your post data, simply create another variable and assign a value to it based off the state value like so:

$stateText = '';
switch ($state){
    case 10:
    $stateText = 'Excellent';
    break;

    case 5:
    $stateText = 'Good';
    break;

    case 0:
    $stateText = 'Poor';
    break;

    default:
    // bad value
    $stateText = '';
}

...then store this to the database in a new column.

This is just one of many ways to do this.


You can only do it if you have a lookup, be it an array or in another table that stores the keys and values.

You should be carefuly not to store the post data directly into your database without sanitizing it, otherwise you might become subject to sql injection.


Is there anyway to put the name of the option into the database instead of the value?

There is, but it involves doing it explicitly (converting "10" into "Excellent" before inserting the value) rather than just basically tossing $_POST into the database as-is. You can make this very simple if you are building the <option>s with an array in the first place by reading the the array again and swapping the values with the keys.

$values = array(
    10  =>  'Excellent',
    5   =>  'Good',
    0   =>  'Poor',
);

$post_value = $_POST['state'];
$db_value = $values[$post_value];

// further validation: make sure the array key exists or use a default value
// further usage: build your HTML <options> with this array

However:

If you're going to do that, you're much better off storing the values as numbers and converting them to words when you display them (assuming the numbers do have some meaning). This also allows you to localize by providing translations.

Response to comments:

I would recommend a rating system, like 1 through 5, and calculate your price modifications internally - not directly from the user input or from a hardcoded value (in the database). This allows you to tweak the price changes from within your app, rather than from database values that were created at an earlier time, like if you decide an "Excellent" condition warrants an increase of 11 rather than 10 - unless you specifically want the prices "locked in" permanently at the time the product was posted.

Whatever you do, make sure to validate the input - I can't think of any good reason to use direct user input to calculate prices - it should be done internally based on product ids, and any other conditions. HTML source can be modified on-the-fly to post values you didn't expect from the dropdown.


You can't get it via the HTML form. But you can still do a server side that would map the values to the appropriate condition.

You can use a switch statement or an if statement to map them.

if(value == 10){ 
        $condition = 'Excellent';
             } else {//....}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜