Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8026707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:34:40+00:00 2026-06-04T23:34:40+00:00

Hey All I currently have a problem with my insert php file I have

  • 0

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\">\n<tr>\n");
  // Use $rowset[0] to write the table heading
  foreach ($headrow as $col => $val) {
    printf("<th>%s</th>\n", $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>\n", $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();    
      }
      ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:34:41+00:00Added an answer on June 4, 2026 at 11:34 pm

    If the inserts are always taking place in sequence, I’d use $pdo->lastInsertId() (see: http://www.php.net/manual/en/pdo.lastinsertid.php)

    So, I think this line is wrong:

    $int_event_id = $_GET["EventID"];
    

    I’d write it like this:

    <?
        $stmt = $pdo->prepare('
                  INSERT INTO results (EventName, EventID)
                  VALUES (:EventResultsName, :EventId)
                ');
        $stmt->bindValue(':EventName', $_POST['EventResultsName']);
        $stmt->bindValue(':EventId', $pdo->lastInsertId());
        $stmt->execute();    
    ?>
    

    Note that this assumes the insert into results occurs immediately after the insert into events.

    You could’ve done the same thing without a bind variable using MySQL’s native last_insert_id() function, like this:

    <?
        $stmt = $pdo->prepare('
                  INSERT INTO results (EventName, EventID)
                  VALUES (:EventResultsName, last_insert_id())
                ');
        $stmt->bindValue(':EventName', $_POST['EventResultsName']);
        $stmt->execute();    
    ?>
    

    However, this is less portable than the previous example. However, pdo’s lastInsertId() isn’t exactly RDBMS agnostic either (see docs) so you’d have to fix this piece of code anyway if you’re thinking of targeting another RDBMS

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, I have an HTML form on my website which currently only uses
Hey all, I have an array that holds all of the .aif file paths
Hey All, I currently use Google's API to include jQuery into my sites like
Hey all, here is the issue I am currently trying to work through. We
Hey all. I'm wondering how to implement a timeout event in the System.Windows.Forms.Webbrowser control?
Hey all. Here is the scenario. I have the below code, and I'm needing
Hey all. I'm reading from one sql-format file to another, and two bytes in
Hey all, I'm currently working on a Java Swing application and I was looking
Hey all, I'm currently teaching myself Objective C and I'm kind of stuck. I'm
hey all. I have a table in my DB that has about a thousand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.