Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...