0

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

-播放器

-球员以前的俱乐部

-以前的俱乐部

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

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

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

有任何想法吗?

4

1 回答 1

1

为插入的球员和俱乐部行生成的 ID$pdo->lastInsertId()在您执行每个查询后可用。因此,在 each 之后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

于 2012-08-20T22:18:06.780 回答