我有一个像这样设置的MySQL表:
-applications (table)
--positionsApplied (column)
在前端应用程序中,您可以检查多个位置,然后将这些位置发送到数据库。
如何处理一个字段的多个值?
这是我的PHP:
<?php
include 'vbs.php';
if (!$connect) {
die('Could not connect: ' . mysql_error());
} else {
echo 'Connected.';
}
$date = date('m-d-Y');
$time = getTime();
//personal
$name = explode(" ", $_POST['name']);
$address = $_POST['address'];
$phone = $_POST['phone'];
$email = $_POST['email'];
//academic
$major = $_POST['major'];
$gradDate = $_POST['gradDate'];
$relevantWork = $_POST['relevantWork'];
//tell us more
$previousExp = $_POST['previousExp'];
$creativeStrengths = $_POST['creativeStrengths'];
$positions = $_POST['position'];
$interests = $_POST['interest'];
$skills = $_POST['skills'];
$workStudy = $_POST['work-study'];
$stmt = $connect->prepare("INSERT INTO `applications` (dateSubmitted, timeSubmitted, firstName,lastName,address,phone,email,major,gradDate,work,
previousExperience,creativeStrengths,interests,skills,workStudy) VALUES ('".$date."', '".$time."', '".$name[0]."', '".$name[1]."','".$address."',
'".$phone."', '".$email."', '".$major."', '".$gradDate."', '".$relevantWork."', '".$previousExp."', '".$creativeStrengths."', '".$positions."',
'".$interests."', '".$skills."', '".$workStudy."')");
if (!$stmt) {
echo "Prepare failed: (" . $connect->errno . ") " . $connect->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
function getTime() {
date_default_timezone_set('America/New_York');
$currenttime = date('h:i:s:u');
list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
return "$hrs:$mins";
}
?>
我的HTML工作位置复选框:
<input name="position" value="Project Manager" type="checkbox" /> Project Manager<br><input name="position" value="Content Developer" type="checkbox" /> Content Developer<br><input name="position" value="Graphic Designer" type="checkbox" /> Graphic Designer<br>
<input name="position" value="Digital Media Creator & Storyteller" type="checkbox" /> Digital Media Creator & Storyteller<br><input name="position" value="Programmer" type="checkbox" /> Programmer<br><input name="position" value="Server Engineer/Technician" type="checkbox" /> Server Engineer/Technician<br>
<input name="position" value="User Experience Designer" type="checkbox" /> User Experience Designer<br>
答案 0 :(得分:0)
您可以使用<input name="position[]" multiple>
,然后使用从select语句中检索到的<option>
填充输入。然后,您可以在PHP中以数组的形式访问多个输入。确保输入的名称后面有[]
,否则它不会被PHP视为数组。