Im currently constructing a database using PHP, HTML and MySQL.
I have an events table. Event_id
is the primary key. When someone registers for an event they input their student_id
and they also select an event from a dropbox that pulls the events from my events table. I have an event_registration
table which records all entries into this form. I have event_id
in my event_registration
table and I'm trying to make it so that when someone selects a certain event, that event's id automatically goes into the event_registration
table. My PHP code at present is this:
$student_id = $_POST['student_id'];
$title = $_POST['title'];
$sql = "select event_id from events where title LIKE $title";
$db->select_db($database);
$event_id = $db->query($sql);
$q = "INSERT INTO event_registration (";
$q .= "student_id, event_id, title";
$q .= ") VALUES (";
$q .= "'$student_id', '$event_id', '$title')";
$result = $db->query($q);
The student_id
, title
, and registraion_id
(auto- increment) are inserting fine, but event_id
is always showing up as 0
, I'm not sure where I'm going wrong. Any help is appreciated.
<td style="width: 176px; height: 23px">Event Title</td>
<td style="height: 23px"><select name="title" style="width: 124px">
<?php
include ("detail.php");
$sql = mysqli_query($db, "SELECT title FROM events");
while ($row = $sql->fetch_assoc()){
?>
<option><?php echo ($row['title']); ?></option>";
<?php
}
?>
</select></td>
my dropdown code