I have a form which allows me to insert a domain to a 'domains' table in my database.
Part of the form includes a list of services that I provide for that domain which are presented as a series of checkboxes and handled as an array. These services are inserted into a marketing_lookup table that has two columns the has the domain id and the service id.
I'm trying to rewrite mysql insert statements using PDO.
I can code inserting the domain to the domains table.
I need help inserting the services array into the marketing_lookup table. The services
The html form on my page
<form ....>
...
<input type='checkbox' name='services[]' value='1'>Service 1<br>
<input type='checkbox' name='services[]' value='2'>Service 2<br>
...
</form>
I have copied and pasted and edited this so far
...
code inserting the domain into the domain table here
...
//start inserting services here
if ($services == '') $services = array();
$services = $_POST['services'];
$id = $conn->lastInsertId(); //obtained from above
if (!isset($_POST['services'])):
echo 'Nothing Selected';
else:
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('INSERT IGNORE INTO marketing_lookup SET
`domain_id` = :id,
`service_id` = :serviceid')
foreach ($services as $serviceid) {
$a = array (':1'=>$serviceid['1'],
':2'=>$serviceid['2']);
if ($stmt->execute($a)) {
//Query succeeded
}
else {
// Query failed.
echo $q->errorCode();
}
// close the database connection
$conn = null;
} // end foreach
} //end try
catch(PDOException $e) {
echo $e->getMessage();
}
endif;
?>