【问题标题】:How to populate a look up table with a dual primary key?如何使用双主键填充查找表?
【发布时间】:2012-08-16 06:31:27
【问题描述】:

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

-播放器

-玩家以前的俱乐部

-以前的俱乐部

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

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

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

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

该表由两个字段组成。一个是 player 表的主键值,另一个是之前的 clubs 表的主键。

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

非常感谢任何有关如何执行此操作的帮助。

谢谢

更新

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

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 值。

有什么想法吗?

【问题讨论】:

标签: php sql


【解决方案1】:

执行每个查询后,为插入的球员和俱乐部行生成的 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-insert-id

【讨论】:

  • 嗨,丹,感谢您的回复。快到了,但是在尝试将存储 id 的变量分配给第二个表时出现错误。请参阅我对原始帖子的更新。奇怪的是,在links表上使用时不起作用。
  • 您将$one$two 直接放在查询中,而不是占位符。然后,您尝试将值绑定到您未创建的占位符。您还尝试绑定不存在的 $_POST 中的键,而不是值所在的 $one$two。我认为您复制和粘贴太多而没有考虑代码的作用:)
  • 嗨,丹,感谢您的回复,您已经按照这个问题的标题回答了这个问题,对此非常抱歉,感谢您的额外建议。
猜你喜欢
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
相关资源
最近更新 更多