MySQL 2 element Database insert failing
Posted: Thu Feb 26, 2009 2:15 pm
Hi Again,
I am struggling to spot the error in my code here. Having reached the dizzying heights of coding in PHP for 7 days now, I have my simple app almost completely working and this is the last one I need to get fixed immediately before I can tell the audience about it. I think its the way I'm inserting data into the database, that is failing. Its a Mysql database.
The source code is:
This calls a function called add_attendee, which is below:
mysql> desc attendees;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| person_id | int(11) | NO | PRI | 0 | |
| event_id | int(11) | NO | PRI | 0 | |
+-----------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)
What am I doing wrong with the DB insert. I just end up with a blank page in the browser and if I refresh it, the DB insert hasn't happened. The table they are writing to is just paired number sets, which link the events and persons tables.
I am struggling to spot the error in my code here. Having reached the dizzying heights of coding in PHP for 7 days now, I have my simple app almost completely working and this is the last one I need to get fixed immediately before I can tell the audience about it. I think its the way I'm inserting data into the database, that is failing. Its a Mysql database.
The source code is:
Code: Select all
<label for="event_id"><b>Event:</b></label><br />
<select name="event_id[]" size="10" multiple="multiple" id="event_id">
<?php foreach($events as $event) {
echo "<option value='{$event->event_id}'>{$event->name} ";
echo date("D jS M, Y", strtotime($event->date))."</option>";
}
?>
</select>
</td>
<td valign="top">
<label for="person_id">Attendees:</label><br />
<select name="person_id[]" size="10" multiple="multiple" id="person_id">
<?php foreach($people as $person) {
echo "<option value='{$person->person_id}'>{$person->first_name} {$person->last_name}</option>";
}
?>
</select>Code: Select all
if(isset($_POST['add_attendee'])) {
if((isset($_POST['person_id']) && $_POST['person_id'] != "")
&& (isset($_POST['event_id']) && $_POST['event_id'] != "")) {
foreach($_POST['person_id'] as $person_id) {
foreach ($_POST['event_id'] as $event_id) {
$save_result = $events[$_POST['event_id']]->add_attendee($person_id);
}
if(!$save_result) {
$errors[] = "A problem occurred adding this attendee";
}
}
}
else {
$errors[] = "To add a attendee, please enter person and an event.";
}
}+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| person_id | int(11) | NO | PRI | 0 | |
| event_id | int(11) | NO | PRI | 0 | |
+-----------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)
What am I doing wrong with the DB insert. I just end up with a blank page in the browser and if I refresh it, the DB insert hasn't happened. The table they are writing to is just paired number sets, which link the events and persons tables.