如何使用双主键填充查找表


How to populate a look up table with a dual primary key?

我有一个数据库,其中包括三个表,如下所示:

-播放器

-球员以前的俱乐部

-以前的俱乐部

"球员以前的俱乐部"表是一个查找表,因为球员和以前的俱乐部之间存在多对多的关系。

现在我有了下面的代码,它成功地从一个表单中填充了"球员"表和"以前的俱乐部表"。

if (isset($_GET['addform']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
//PLAYER TABLE INSERT
try
{
    $sql = 'INSERT INTO player SET
            name = :name,
            age = :age,
            position = :position,
            height = :height,
            weight = :weight,
            satscore = :satscore,
            gpa = :gpa';
    $s = $pdo->prepare($sql);
    $s->bindValue(':name', $_POST['name']);
    $s->bindValue(':age', $_POST['age']);
    $s->bindValue(':position', $_POST['position']);
    $s->bindValue(':height', $_POST['height']);
    $s->bindValue(':weight', $_POST['weight']);
    $s->bindValue(':satscore', $_POST['satscore']);
    $s->bindValue(':gpa', $_POST['gpa']);
    $s->execute();
}
catch (PDOException $e)
{
    $error = 'Error adding submitted player profile.' . $e->getMessage();
    include 'error.html.php';
    exit();
}
//PREVIOUS CLUBS TABLE INSERT
try
{
    $sql = 'INSERT INTO previousclubs SET
            name = :previousclubs';
    $s = $pdo->prepare($sql);
    $s->bindValue(':previousclubs', $_POST['previousclubs']);
    $s->execute();
}
catch (PDOException $e)
{
    $error = 'Error adding previous club.' . $e->getMessage();
    include 'error.html.php';
    exit();
}
header('Location: .');
exit();
}

我不确定如何填充查找表(球员之前的俱乐部),因为它所需的值不是通过表格传递的,而是通过自动递增功能生成的。

该表由两个字段组成。一个是球员表的主键值,另一个是上一个俱乐部表的主键。

这两个字段共同构成一个联合主键。

非常感谢您对如何做到这一点的任何帮助。

感谢

更新

好的,我现在有以下代码:

if (isset($_GET['addform']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
//PLAYER TABLE INSERT
try
{
    $sql = 'INSERT INTO player SET
            name = :name,
            age = :age,
            position = :position,
            height = :height,
            weight = :weight,
            satscore = :satscore,
            gpa = :gpa';
    $s = $pdo->prepare($sql);
    $s->bindValue(':name', $_POST['name']);
    $s->bindValue(':age', $_POST['age']);
    $s->bindValue(':position', $_POST['position']);
    $s->bindValue(':height', $_POST['height']);
    $s->bindValue(':weight', $_POST['weight']);
    $s->bindValue(':satscore', $_POST['satscore']);
    $s->bindValue(':gpa', $_POST['gpa']);
    $s->execute();
    $one = $pdo->lastInsertId();
}
catch (PDOException $e)
{
    $error = 'Error adding submitted player profile.' . $e->getMessage();
    include 'error.html.php';
    exit();
}
//PREVIOUS CLUBS TABLE INSERT
try
{
    $sql = 'INSERT INTO previousclubs SET
            name = :previousclubs';
    $s = $pdo->prepare($sql);
    $s->bindValue(':previousclubs', $_POST['previousclubs']);
    $s->execute();
    $two = $pdo->lastInsertId();
}
catch (PDOException $e)
{
    $error = 'Error adding previous club.' . $e->getMessage();
    include 'error.html.php';
    exit();
}
//PLAYER PREVIOUS CLUB INSERT
try
{
    $sql = "INSERT INTO playerpreviousclubs SET
            playerid = '$one',
            previousclubid = '$two'";
    $s = $pdo->prepare($sql);
    $s->bindValue(':playerid', $_POST['playerid']);
    $s->bindValue(':previousclubid', $_POST['previousclubid']);
    $s->execute();
}
catch (PDOException $e)
{
    $error = 'Error adding player previous club look up data.' . $e->getMessage();
    include 'error.html.php';
    exit();
}
//LINKS TABLE INSERT
try
{
    $sql = "INSERT INTO links SET
            link = :link,
            playerid = '$one'";
    $s = $pdo->prepare($sql);
    $s->bindValue(':link', $_POST['link']);
    $s->bindValue(':playerid', $_POST['playerid']);
    $s->execute();
}
catch (PDOException $e)
{
    $error = 'Error adding link for player.' . $e->getMessage();
    include 'error.html.php';
    exit();
}
header('Location: .');
exit();
}

这给了我以下错误:

注意:第84行上的C:''Program Files(x86)''Apache Software Foundation''Apache2.2''htdocs''connect''players''index.php中的未定义索引:playerid

注意:未定义的索引:第85行的C:''Program Files(x86)''Apache Software Foundation''Apache2.2''htdocs''connect''players''index.php中的previousclubid

注意:未定义的索引:在C:''Program Files(x86)''Apache Software Foundation''Apache2.2''htdocs''connect''players''index.php中的playerid,位于第103行

为玩家添加链接时出错。SQLSTATE[HY093]:参数编号无效:绑定变量的数量与令牌的数量不匹配

如果我删除了"//LINKS TABLE INSERT"下的所有代码,那么它就可以工作了,所以我认为问题就在这里,但我也需要这个表来存储$one值。

有什么想法吗?

执行每个查询后,为插入的球员和俱乐部行生成的ID在$pdo->lastInsertId()中可用。因此,在每次execute()之后,将插入的ID存储在一个变量中,以便在以后的查询中使用。

http://php.net/manual/en/pdo.lastinsertid.php

http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-插入id