简体   繁体   English

如何从3个字段(月,日,年)中获取值,并将所有这三个字段插入为单个字段(MySQL)?

[英]How do I take values from 3 fields (Month, Day, Year) and have all three of these fields inserted as a single field (MySQL)?

I've created an HTML form and its corresponding insert.php code. 我创建了一个HTML表单及其相应的insert.php代码。 The question I have is this: 我的问题是:

1) Does PHP read the ID or NAME field (HTML) when the user presses submit on an HTML form? 1)当用户在HTML表单上按下Submit时,PHP是否读取ID或NAME字段(HTML)?
2) How do I use PHP to combine fields so that it is inserted into a MySQL Database in a single column/field? 2)如何使用PHP组合字段,以便将其插入到MySQL数据库中的单个列/字段中? (I'm trying to be more specific; last few Questions of mine were flagged on Stack, so I'm -trying- to adhere to the community standard) (我正在尝试更具体;我的最后几个问题已在Stack上标记,因此我正在-尝试-遵守社区标准)

<label class="description" for="element_23">Date of Birth </label>
    <span>
        <input id="element_23_1" name="applicants.DOB_month" class="element text" size="2" maxlength="2" value="" type="text"> /
        <label for="element_23_1">MM</label>
    </span>
    <span>
        <input id="element_23_2" name="applicants.DOB_day" class="element text" size="2" maxlength="2" value="" type="text"> /
        <label for="element_23_2">DD</label>
    </span>
    <span>
        <input id="element_23_3" name="applicants.DOB_year" class="element text" size="4" maxlength="4" value="" type="text">
        <label for="element_23_3">YYYY</label>
    </span>

--MySQL-- --MySQL--

$sql = "INSERT INTO applicants (fname, lname, address, city, state, zip, country, phone, school, DOB, age, amount_requested) VALUES ('$applicants_fname', '$applicants_lname', '$applicants_address','$applicants_city','$applicants_state','$applicants_zip','$applicants_country','$applicants_phone','$applicants_school','$DOB','$age', '$applicants_amount_requested')";

To create 1 field in DB from 3 fields with data from your form you can get all this 3 fields (from $_POST оr $_GET array (this param must be in <form>s action attribute )): 要从3个字段在DB中使用表单数据创建1个字段,您可以获取所有这3个字段(从$_POST$_GET数组(此参数必须位于<form>s action attribute )):

$value = $_POST['field1_name'].'/'.$_POST['field2_name'].' /'.$_POST['field3_name'];

and then insert it into DB. 然后将其插入数据库。

Assign a new variable and concatenate the ones you want to be entered in the db, then use that variable as the final product. 分配一个新变量,并将要输入的变量串联起来,然后将该变量用作最终产品。

Ie: and assuming a POST form method, since you did not specify that in your question. 即:并假设采用POST表单方法,因为您未在问题中指定。

$year = $_POST['applicants.DOB_year'];
$month = $_POST['applicants.DOB_month'];
$day = $_POST['applicants.DOB_day'];

$DOB = $year . "-" . $month . "-" . $day;

$sql = "INSERT INTO applicants 
        (fname, lname, address, city, state, zip, country, phone, school, DOB, age, amount_requested) 
        VALUES ('$applicants_fname', '$applicants_lname', '$applicants_address','$applicants_city',
        '$applicants_state','$applicants_zip','$applicants_country','$applicants_phone','$applicants_school',
        '$DOB','$age', '$applicants_amount_requested')";

You'll also want to set your column as a DATE type since MySQL stores that as YYYY-MM-DD. 您还需要将列设置为DATE类型,因为MySQL将其存储为YYYY-MM-DD。

The reason being that it will be easier to query later. 原因是以后查询起来会更容易。 You will have a harder time if your column is set to VARCHAR and would have to result in using more functions/resources than what is required. 如果将列设置为VARCHAR,您将更加费时,并且将导致使用的函数/资源超过要求的数量。

  • Use MySQL's built-in DATE functions. 使用MySQL的内置DATE函数。

The above will render something like 1995-12-22 上面将呈现类似1995-12-22的内容

From the manual: 从手册中:

The DATE type is used for values with a date part but no time part. DATE类型用于具有日期部分但没有时间部分的值。 MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. MySQL检索并以'YYYY-MM-DD'格式显示DATE值。 The supported range is '1000-01-01' to '9999-12-31'. 支持的范围是“ 1000-01-01”至“ 9999-12-31”。

You should also use a prepared statement, since your code may be prone to an SQL injection. 您还应该使用一条准备好的语句,因为您的代码可能易于进行SQL注入。 It is unknown if you are escaping your data. 是否要转义数据是未知的。

"1) Does PHP read the ID or NAME field (HTML) when the user presses submit on an HTML form?" “ 1)当用户在HTML表单上按下Submit时,PHP是否读取ID或NAME字段(HTML)?”

If you're using pure PHP, then it relies on the "name" attribute. 如果您使用的是纯PHP,则它依赖于“名称”属性。

JS/Ajax however, does support the ID attribute though. 但是,JS / Ajax确实支持ID属性。

it depends of your column type but if this is a string, 这取决于您的列类型,但是如果这是字符串,
you can use MySQL CONCAT 您可以使用MySQL CONCAT

INSERT INTO `table`(`field`) VALUES (CONCAT('val1', '/', 'val2', '/', 'val3'));
  1. on form submit, the request string is created from the name fields, not the id . 在提交表单时,请求字符串是从name字段而不是id Thus the request is name1=value1&name2=value2 .... 因此,请求为name1=value1&name2=value2 ...。
  2. You have to create the valid value to insert in the right field. 您必须创建有效值才能插入右侧字段。 You can use php and concatenate values $values = $_POST['name1'] . 'delimiter' . $_POST['name2'] 您可以使用php并连接$values = $_POST['name1'] . 'delimiter' . $_POST['name2'] $values = $_POST['name1'] . 'delimiter' . $_POST['name2'] $values = $_POST['name1'] . 'delimiter' . $_POST['name2'] or you can use the SQL CONCAT function. $values = $_POST['name1'] . 'delimiter' . $_POST['name2'] ,也可以使用SQL CONCAT函数。

暂无
暂无

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

相关问题 如何将“高级自定义字段”日期选择器中的“月”,“日”和“年”字段分成单独的跨度或div? - How do I separate the Month, Day, and Year fields from Advanced Custom Fields datepicker into a individual spans or divs? 如何将Facebook生日(mm / dd / aaaa)分为3个不同的值,并将它们分配给3个不同的字段(日,月,年) - How to split Facebook birthday (mm/dd/aaaa) into 3 different values and assign them to 3 different fields (day, month, year) 在MySQL中,如何从具有三个字段的搜索表单中检索多个值? - In MySQL how do retrieve multiple values from a Search form with three fields? 并非所有字段都插入到MySQL表中 - not all fields are inserted into MySQL table 如何使用PHP从MySQL表中的同一行中的年和月字段排序 - How to sort year and month fields in same row from mysql table using php mysql按月和年作为单独的字段进行搜索 - mysql search by month and year as separate fields 在laravel中查看三个输入字段中的日期作为日月和年 - view date in three input field as day month & year in laravel 获取数据库中的日期时间并在Dreamweaver中的3个单独字段中显示[年,月,日] - Get datetime in a database and show it in 3 separate fields in dreamweaver [ year, month, day ] 从 MYSQL 上的时间戳中提取日/月/年 - Extract the Day / Month / Year from a Timestamp on MYSQL 筛选/排序具有三个关联值(MONTH,DAY和YEAR)的值 - Filter/Sort through a value with three associated values (MONTH, DAY, and YEAR)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM