我如何从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)?

我已经创建了一个HTML表单及其相应的insert.php代码。我的问题是:

1)当用户在HTML表单上按下submit时,PHP是否读取ID或NAME字段(HTML) ?
2)我如何使用PHP来组合字段,使其插入到MySQL数据库中的单列/字段?(我试着说得更具体;我的最后几个问题在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——

$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')";

要在数据库中从3个字段中创建1个字段,您可以从表单中获得所有这3个字段(来自$_POST, $_GET数组(此参数必须在<form>s action attribute中)):

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

这取决于你的列类型但如果这是一个字符串,
你可以使用MySQL的CONCAT

INSERT INTO `table`(`field`) VALUES (CONCAT('val1', '/', 'val2', '/', 'val3'));
  1. 表单提交时,请求字符串是从name字段创建的,而不是id。因此请求是name1=value1&name2=value2 ....
  2. 您必须创建要插入到右侧字段的有效值。您可以使用php和$values = $_POST['name1'] . 'delimiter' . $_POST['name2']连接值,或者您可以使用SQL CONCAT函数。

指定一个新变量,并将您想要在数据库中输入的变量连接起来,然后使用该变量作为最终产品。

。:并假设一个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')";

您还需要将列设置为DATE类型,因为MySQL将其存储为YYYY-MM-DD。

  • https://dev.mysql.com/doc/refman/5.1/en/datetime.html

原因是以后查询起来更容易。如果您的列被设置为VARCHAR,您将会遇到困难,并且不得不使用比所需更多的函数/资源。

    使用MySQL的内置DATE函数。

上面会显示类似1995-12-22的内容

从手册:

DATE类型用于有日期部分但没有时间部分的值。MySQL以'YYYY-MM-DD'格式检索和显示DATE值。支持的范围为' 10001-01-01 ' ~ ' 99999-12-31 '。

您还应该使用准备好的语句,因为您的代码可能容易发生SQL注入。如果您正在转义您的数据,则未知。

  • https://en.wikipedia.org/wiki/Prepared_statement

"1)当用户在HTML表单上按提交时,PHP是否读取ID或NAME字段(HTML) ?"

如果你使用的是纯PHP,那么它依赖于"name"属性。

  • 咨询http://php.net/manual/en/tutorial.forms.php
但是JS/Ajax支持ID属性。
  • 咨询http://webdesign.tutsplus.com/tutorials/building - -引导-接触-形式使用ajax - php -和- cms - 23068
相关文章: