I have a database and this includes three tables as follows:
-Player
-Player Previous Clubs
-Previous Clubs
The ‘Player Previous Clubs’ table is a look up table as there is a many-to-many relationship between player and previous clubs.
Now I have the below code which successfully populates the ‘player’ table and ‘previous clubs table’ from a single form.
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();
}
I am unsure of how to populate the look up table (player previous clubs) as the values that it requires are not passed in via the form, they are generated via the auto increment functionality.
The table consists of two fields. One is the primary key value of the player table and the other is the primary key of the previous clubs table.
Together these two fields form a joint primary key.
Any help on how to do this is greatly appreciated.
Thanks
UPDATE
Okay I now have the below code:
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();
}
This gives me the following errors:
Notice: Undefined index: playerid in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\connect\players\index.php on line 84
Notice: Undefined index: previousclubid in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\connect\players\index.php on line 85
Notice: Undefined index: playerid in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\connect\players\index.php on line 103
Error adding link for player.SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
If I remove all of the code under ‘//LINKS TABLE INSERT’ then it works so I assume the problem is here but I need this table to store the $one value also.
Any ideas?
The ID generated for the inserted player and club rows are available in
$pdo->lastInsertId()after you execute each of those queries. So after eachexecute(), store the inserted ID in a variable to use in the later query.http://php.net/manual/en/pdo.lastinsertid.php
http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id