[英]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,您将更加费时,并且将导致使用的函数/资源超过要求的数量。
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'));
name
fields, not the id
. name
字段而不是id
。 Thus the request is name1=value1&name2=value2
.... name1=value1&name2=value2
...。 $values = $_POST['name1'] . 'delimiter' . $_POST['name2']
$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.