This is based on your code.
It allows up to 5 emails to be entered. It validates them and displays individual error messages. It prevents duplicate emails being entered on the form.
The database query is generated for as many columns as are input.
The array: $emailDetails holds all the information about the individual emails.
the test: strlen(implode($_POST['email']) ensures that the input array has at least one value in it.
Tested: PHP 5.3.18 windows XP.
<?php // Q22885105 - example tested code.
/*
* this is an example of how to generate the query and the bind values...
*
* You will need to modify it for your use case.
*
* This script allows 5 'email' to be entered and stored
*/
/*
* Do we have some email input? -- do some validation
*/
$badEmailCount = 0; // assume all the 'email' are correct
$emailDetails = array(); // store email info in here
// use $emailDetails['isValid'][0] - to check if all ok!
// use $emailDetails['value'][0] - to get the value
//
// let us make life easier for us all and ensure that there are always 5 'email'!
for($idx = 0; $idx < 5; $idx++) {
$emailDetails['isValid'][$idx] = TRUE; // must be true!
$emailDetails['value'][$idx] = '';
$emailDetails['htmlId'][$idx] = "email_$idx";
$emailDetails['colName'][$idx] = "email$idx";
$emailDetails['error'][$idx] = "";
}
if (!empty($_POST['email']) && strlen(implode($_POST['email'])) >= 1) { // validate email input
for($idx = 0; $idx < 5; $idx++) {
if (!empty($_POST['email'][$idx])) {
$isBad = !filter_var($_POST['email'][$idx], FILTER_VALIDATE_EMAIL);
if ($isBad) {
$emailDetails['error'][$idx] = 'is bad email address';
}
else { // duplicate check
foreach($_POST['email'] as $idxDup => $dupValue) {
$isBad = $idxDup !== $idx && $dupValue == $_POST['email'][$idx];
if ($isBad) {
$emailDetails['error'][$idx] = 'is duplicated email address';
break;
}
}
}
if ($isBad) {
$badEmailCount++;
}
$emailDetails['isValid'][$idx] = !$isBad;
$emailDetails['value'][$idx] = $_POST['email'][$idx];
}
}
}
else { // do we have the form but is it empty?
if (!empty($_POST['email']) && strlen(implode($_POST['email'])) == 0) {
$emailDetails['isValid'][0] = false;
$emailDetails['error'][0] = 'one email address is needed';
$badEmailCount++;
}
} // end validation...
?>
<!-- generate HTML code for the email form -->
<?php if (empty($_POST['goEmail']) || $badEmailCount > 0): // no input or has error - show the form... ?>
<form action="" method="post"
<fieldset class="email">
<legend>Email details please...</legend>
<?php for($idx = 0; $idx < 5; $idx++): ?>
<div style="margin: 2px;<?php echo !$emailDetails['isValid'][$idx] ? ' border: 2px solid red;' : '';?> ">
<label for id="<?php echo $emailDetails['htmlId'][$idx]?>"><?php echo $emailDetails['colName'][$idx]?></label>
<input type="text" name="email[]" id="<?php echo $emailDetails['htmlId'][$idx]?>"
value="<?php echo $emailDetails['value'][$idx] ?>">
<?php echo !$emailDetails['isValid'][$idx] ? $emailDetails['error'][$idx] : ''; ?>
</div>
<?php endfor; ?>
</fieldset>
<input type="submit" name="goEmail" value="tell us your thoughts...">
</form>
<?php endif; ?>
<?php
if (empty($_POST['goEmail']) || $badEmailCount > 0) {
exit; // leave the script now...
}
// continue processing the input data
// database connection...
$dsn = 'mysql:host=localhost;dbname=testmysql';
$username = 'test';
$password = 'test';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$theDB = new PDO($dsn, $username, $password, $options);
// make db/pdo throw an exception when it gets confused.
$theDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// ------------------ end of database connection --------
// get input form details...
$emailQueryValues = array();
$sqlColumns = '';
$sqlBindings = '';
for($idx = 0; $idx < 5; $idx++) {
if (!empty($emailDetails['value'][$idx])) {
$sqlColumns .= '`'. $emailDetails['colName'][$idx] .'`,';
$sqlBindings .= ':'. $emailDetails['colName'][$idx] .',';
$emailQueryValues[':'. $emailDetails['colName'][$idx]] = $emailDetails['value'][$idx];
}
}
$sqlColumns = rtrim($sqlColumns, ', '); // lose trailing comma
$sqlBindings = rtrim($sqlBindings, ', ');
try {
$sql = "insert into uitnodigen ($sqlColumns) values ($sqlBindings)";
$query = $theDB->prepare($sql);
$query->execute($emailQueryValues);
$lastId = $theDB->lastInsertId();
}
catch (\Exception $e) {
echo 'drat! '. $e->getMessage();
// throw $e; // re-raise the exception
}
// test it worked...
$sql = 'select * from uitnodigen where id = :id';
$query = $theDB->prepare($sql);
$query->execute(array(':id' => $lastId));
$resultSet = $query->fetchAll();
var_dump(current($resultSet));
?>