Hey All I currently have a problem with my insert php file
I have a form.. where the user types in details.. for my form.. event name, event date and location
basically the bit that is working
is
well first of all i would like to add an entry into 2 tables: events and results
it's having no problems adding the entry into events
but it doesnt add the same entry into "results"
the events table had the following columns: Event ID, Event Name, Event Date and Location
The Results table has: Event ID, Member ID, Event Name, Score and Place
The Event ID is auto increment
so it auto assigns an ID to it
and its applied to both tables
the auto increment in Event ID
the bit thats working is
inserting entry into the events table
but because the events table and results table both have "Event Name
I want this php to fully insert details for the event table
BUT also at the same time, just insert the eventname into the results table
but the EventID in events has to be the same generated number as EventID in results..
Below is my code: All help really appreciated!!!
<?
$pdo = new PDO('mysql:host=localhost;dbname=clubresults', 'root', '12345678');
#Set Error Mode to ERRMODE_EXCEPTION.
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $pdo->query('SELECT EventID, EventName, EventDate, Location from events');
$rowset = array();
if ($query) {
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
// Build array of rows
$rowset[] = $row;
}
// Output header first
$headrow = $rowset[0];
print("<table border="1">
<tr>
");
// Use $rowset[0] to write the table heading
foreach ($headrow as $col => $val) {
printf("<th>%s</th>
", $col);
}
print("</tr>");
// Then output table rows.
// Outer loop iterates over row
foreach ($rowset as $row) {
print("<tr>");
// Inner loop iterates over columns using $col => $val
foreach ($row as $col => $val) {
// We don't know your column names, but substitute the first column (the ID) for FIRSTCOL here
printf("<td><a href="adminlistresults.php?EventID=%s">%s</a></td>
", $row['EventID'],$val);
}
print("</tr>");
}
}
print("</table>");
?>
</form>
</div>
<?
$pdo = new PDO('mysql:host=localhost;dbname=clubresults', 'root', '12345678');
#Set Error Mode to ERRMODE_EXCEPTION.
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt=$pdo->prepare('INSERT INTO events (EventName, EventDate, Location)
VALUES (:EventName, :EventDate, :Location)');
$stmt->bindValue(':EventName', $_POST['EventName']);
$stmt->bindValue(':EventDate', $_POST['EventDate']);
$stmt->bindValue(':Location', $_POST['Location']);
$stmt->execute();
?>
<?
$int_event_id = $_GET["EventID"];
if((int)$int_event_id)
{
$stmt=$pdo->prepare('INSERT INTO results (EventName, EventID)
VALUES (:EventResultsName, $int_event_id)');
$stmt->bindValue(':EventName', $_POST['EventResultsName']);
$stmt->execute();
}
?>
See Question&Answers more detail:
os