简体   繁体   English

PHP表单将值发布到数据库

[英]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. 他们选择的选项的值在末尾加起来,得到报价-在上面的示例中-“ 10”表示状况良好的笔记本电脑等的£10英镑。

I use $_POST[state] to get the value of it to add onto the other options for the quote. 我使用$ _POST [state]来获取它的值,并将其添加到报价的其他选项上。

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". 当它们添加到数据库时,显然它只是作为值而不是像“ excellent”或“ good”之类的实际名称出现。 just says "10" or "5". 只需说“ 10”或“ 5”。

Is there anyway to put the name of the option into the database instead of the value? 无论如何,有没有将选项名称而不是值放入数据库中?

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. 有,但是它涉及显式地执行此操作(在插入值之前将“ 10”转换为“ Excellent”),而不仅仅是将$_POST基本上按原样扔到数据库中。 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. 如果首先使用数组构建<option>则可以使其非常简单,方法是再次读取数组并用键交换值。

$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). 我建议使用一种评级系统,例如1到5,并在内部计算您的价格修改-而不是直接从用户输入或硬编码值(在数据库中)中计算。 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. 这使您可以从应用程序内部而不是从较早时间创建的数据库值中调整价格变化,例如,如果您确定“优秀”条件需要将价格提高11而不是10-除非您特别想要价格产品发布时永久“锁定”。

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. 无论您做什么,都要确保对输入进行验证-我想不出有任何充分的理由使用直接用户输入来计算价格-应该根据产品ID和任何其他条件在内部完成。 HTML source can be modified on-the-fly to post values you didn't expect from the dropdown. 您可以即时修改HTML源代码,以发布您在下拉菜单中未曾期望的值。

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. 您应该小心,不要在不进行清理的情况下将帖子数据直接存储到数据库中,否则可能会受到sql注入的影响。

You can't get it via the HTML form. 您无法通过HTML表单获取它。 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. 您可以使用switch语句或if语句来映射它们。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM