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

时间:2012-08-20 22:10:21

标签: php sql

我有一个数据库,包括以下三个表:

单放机

-Player以前的俱乐部

- 以前的俱乐部

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

现在我有以下代码成功填充了“玩家”#39;桌子和之前的俱乐部桌子'从单一形式。

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();
}

我不确定如何填充查找表(播放器以前的俱乐部),因为它所需的值不通过表单传递,它们是通过自动增量功能生成的。

该表包含两个字段。一个是播放器表的主键值,另一个是前一个club表的主键。

这两个字段一起形成一个联合主键。

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

由于

更新

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

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

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

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

如果我删除' // LINKS TABLE INSERT'下的所有代码然后它工作,所以我认为问题在这里,但我需要这个表来存储$ one值。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

执行每个查询后,$pdo->lastInsertId()可以在execute()中找到为插入的玩家和俱乐部行生成的ID。因此,在每个{{1}}之后,将插入的ID存储在变量中,以便在以后的查询中使用。

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

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