更新时间:2023-02-26 10:07:49
如果插入总是按顺序进行,我会使用 $pdo->lastInsertId()
(参见:http://www.php.net/manual/en/pdo.lastinsertid.php)
所以,我认为这一行是错误的:
$int_event_id = $_GET["EventID"];
我会这样写:
$stmt = $pdo->prepare('INSERT INTO 结果(事件名称、事件 ID)值 (:EventResultsName, :EventId)');$stmt->bindValue(':EventName', $_POST['EventResultsName']);$stmt->bindValue(':EventId', $pdo->lastInsertId());$stmt->execute();?>
请注意,这假设在插入到事件之后立即插入到结果中.
您可以使用 MySQL 的原生 last_insert_id()
函数在没有绑定变量的情况下完成同样的事情,如下所示:
$stmt = $pdo->prepare('INSERT INTO 结果(事件名称、事件 ID)值 (:EventResultsName, last_insert_id())');$stmt->bindValue(':EventName', $_POST['EventResultsName']);$stmt->execute();?>
然而,这不如前一个示例可移植.但是,pdo 的 lastInsertId()
也不完全是 RDBMS 不可知的(请参阅文档),因此如果您想针对另一个 RDBMS,无论如何您都必须修复这段代码
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();
}
?>
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