确定。所以我有一个表格应该获取用户信息,包括应该存储在数组中的种族信息以允许多个选择,但它也应该是可选的,这样就没有人有义务进行任何选择
输入页面
种族背景(选择所有适用的选项):
<input type="checkbox" name="ethnicity[]" value="Indian"> American Indian or Alaska Native<br>
<input type="checkbox" name="ethnicity[]" value="Asian"> Asian<br>
<input type="checkbox" name="ethnicity[]" value="African-American"> Black or African-American<br>
<input type="checkbox" name="ethnicity[]" value="Pacific"> Native Hawaiian or other Pacific Islander<br>
<input type="checkbox" name="ethnicity[]" value="White"> White<br>
在我的processData页面上,然后将所有内容存储到变量
中$email = filter_input (INPUT_POST, 'email');
$passwordUser = filter_input (INPUT_POST, 'passwordUser');
$phone = filter_input (INPUT_POST, 'phone');
$sex = filter_input (INPUT_POST, 'sex');
if ($sex == NULL) {
$sex = 'unknown';
}
$age = filter_input (INPUT_POST, 'age');
$state = filter_input (INPUT_POST, 'state');
$ethnicity = filter_input (INPUT_POST, 'ethnicity', FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY);
if ($ethnicity !== NULL) {
} else {
'No answer selected.';
}
include 'insertData.php';
然后在insertData页面上将所有信息插入表格中。
include 'database.php';
try {
$query = "INSERT INTO accounts
(ageGroup, emailAddress, gender, password, phoneNumber, stateAbbr)
VALUES
(:ageGroup, :emailAddress, :gender, :passwordUser, :phoneNumber, :stateAbbr)";
$statement = $db->prepare($query);
$statement->bindValue(':ageGroup', $age);
$statement->bindValue(':emailAddress', $email);
$statement->bindValue(':gender', $sex);
$statement->bindValue(':passwordUser', $passwordUser);
$statement->bindValue(':phoneNumber', $phone);
$statement->bindValue(':stateAbbr', $state);
$statement->execute();
$statement->closeCursor();
foreach ($ethnicity as $result) {
$query = "INSERT INTO customerethnicity
(emailAddress, ethnicity)
VALUES
(:emailAddress, :ethnicity)";
$statement = $db->prepare($query);
$statement->bindValue(':ethnicity', $result);
$statement->bindValue(':emailAddress', $email);
$statement->
execute();
$statement->closeCursor();
}
echo "Account created successfully!";
} catch (PDOException $e) { //Catches and handles db errors to prevent site crash
$title = 'Error';
$caption = 'System message:';
$message = $e->getMessage() . '<br><br> An error has occurred. Please check the information entered and try again.';
include 'Database_error.php';
数据库使用两个由电子邮件链接的表格,电子邮件和种族一起在桌面上创建专门为种族设计的新行。另一个表存储其余信息。几乎所有东西都有效。我可以将性别留空并存储“未知”。在桌子上,但是我不能不让民族盒子离开,并且有一个类似的“未知”。插入表中的值代替NULL而不收到此消息:
警告:在第26行的C:\ xampp \ htdocs \ FormData \ insertData.php中为foreach()提供的参数无效
Account created successfully!
这是第26行:foreach ($ethnicity as $result) {
帐户创建成功的测试仪消息弹出,如果我查看表格,我可以看到输入的所有其他信息显示在帐户表中,但在customerethnicity表中没有显示。如果我填写信息,或者即使我将其他值留空,只选择一个或多个种族,那么一切正常。问题是没有选择种族。我需要显示一个值,表示没有选择任何内容。
答案 0 :(得分:0)
如果条件为未设置的情况,请为复选框添加ID并具有类似的内容:
if (document.getElementById('yourCheckBoxID').checked) {
alert("checked");
} else {
alert("Please check");
}
答案 1 :(得分:0)
您可以简单地测试$ethnicity
&#34;是否&#34;在尝试迭代它之前的数组。
if ( is_array($ethnicity) ) {
$query = '
INSERT INTO customerethnicity
(emailAddress, ethnicity)
VALUES
(:emailAddress, :ethnicity)
';
$statement = $db->prepare($query);
$statement->bindParam(':ethnicity', $result);
$statement->bindParam(':emailAddress', $email);
foreach ($ethnicity as $result) {
$statement->execute();
...
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly', array(
PDO::ATTR_EMULATE_PREPARES=>false,
PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
setup($pdo); // creating temporary tables for the example
// the array is more or less what you got from your filter_input script
// doInsert will extract it, so the code in that function looks like your second script
doInsert($pdo, array(
'age'=>'21-39',
'email'=>'emailA',
'sex'=>'m',
'passwordUser'=>'1234',
'phone'=>'5678',
'state'=>'neverneverland',
'ethnicity'=>['de','fr','au']
));
doInsert($pdo, array(
'age'=>'80+',
'email'=>'emailB',
'sex'=>'m',
'passwordUser'=>'abcd',
'phone'=>'9876',
'state'=>'midleearth',
'ethnicity'=>null
));
// example for querying the data
// not sure if that's a really good way to do it; but ...just an example ;-)
showUser($pdo, 'emailA');
showUser($pdo, 'emailB');
function doInsert($db, $params) {
extract($params);
try {
$db->beginTransaction(); // that should answer your second question, see http://docs.php.net/pdo.transactions
$query = "
INSERT INTO soFoo
(ageGroup, emailAddress, gender, password, phoneNumber, stateAbbr)
VALUES
(:ageGroup, :emailAddress, :gender, :passwordUser, :phoneNumber, :stateAbbr)
";
$statement = $db->prepare($query);
$statement->bindValue(':ageGroup', $age);
$statement->bindValue(':emailAddress', $email);
$statement->bindValue(':gender', $sex);
$statement->bindValue(':passwordUser', $passwordUser);
$statement->bindValue(':phoneNumber', $phone);
$statement->bindValue(':stateAbbr', $state);
$statement->execute();
$statement->closeCursor();
if ( is_array($ethnicity) ) {
$query = '
INSERT INTO soBar
(emailAddress, ethnicity)
VALUES
(:emailAddress, :ethnicity)
';
$statement = $db->prepare($query);
$statement->bindParam(':ethnicity', $result);
$statement->bindParam(':emailAddress', $email);
foreach ($ethnicity as $result) {
$statement->execute();
}
}
$db->commit();
}
catch(Exception $e) {
$db->rollBack();
throw $e;
}
}
function showUser($db, $email) {
$statement = $db->prepare('
(
SELECT
ageGroup,gender,stateAbbr
FROM
soFoo
WHERE
emailAddress=:e1
)
UNION ALL
(
SELECT
ethnicity,null,null
FROM
soBar
WHERE
emailAddress=:e2
)
');
$statement->bindValue(':e1', $email);
$statement->bindValue(':e2', $email);
$statement->execute();
$user = $statement->fetch();
if ( !$user ) {
echo 'no user with email=', $email, "\r\n";
}
else {
printf("%s %s %s\r\n", $user['ageGroup'], $user['gender'], $user['stateAbbr']);
$eth = $statement->fetch();
if ( !$eth ) {
echo ' unknown ethnicity';
}
else {
do {
echo ' ', $eth['ageGroup'], "\r\n";
}
while( ($eth=$statement->fetch()) );
}
}
}
function setup($pdo) {
$pdo->exec('
CREATE TEMPORARY TABLE soFoo (
id int auto_increment,
ageGroup varchar(32),
emailAddress varchar(32),
gender varchar(32),
password varchar(32),
phoneNumber varchar(32),
stateAbbr varchar(32),
primary key(id),
unique key(emailAddress)
)
');
$pdo->exec('
CREATE TEMPORARY TABLE soBar (
id int auto_increment,
emailAddress varchar(32),
ethnicity varchar(32),
primary key(id),
unique key(emailAddress, ethnicity)
)
');
}
打印
21-39 m neverneverland
au
de
fr
80+ m midleearth
unknown ethnicity
答案 2 :(得分:0)
你可以使用
if(isset($_POST['ethnicity'])){
foreach ($ethnicity as $result) {
....
}
}
答案 3 :(得分:0)
延迟的应用,但最终我最终使用的解决方案是:
$ethnicity = filter_input (INPUT_POST, 'ethnicity', FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY);
if ($ethnicity !== NULL) {
} else {
$ethnicity = array('unknown');
}
include 'insertData.php';
?>
希望这可能对其他人有益。