所以我制作了下面的表格,我试图通过insert.php页面将播放器添加到播放器表格中,当我尝试时,我收到以下错误消息:
ErrorCannot添加或更新子行:外键约束失败(
football_db
。player
,CONSTRAINTplayer_ibfk_1
FOREIGN KEY(teamID
)参考team
(teamID
))
这是我的表的sql代码,非常基本:
CREATE TABLE player (
playerID int(3) AUTO_INCREMENT NOT NULL,
playerName VARCHAR(50) NOT NULL,
playerAge int(3) NOT NULL,
playerNation VARCHAR(60) NOT NULL,
playerPosition VARCHAR(50) NOT NULL,
playerTeam VARCHAR(50) NOT NULL,
teamID int(3) NOT NULL,
PRIMARY KEY(playerID),
FOREIGN KEY(teamID) REFERENCES team(teamID)) ENGINE=innodb;
CREATE TABLE team (
teamID int(3) AUTO_INCREMENT NOT NULL,
teamName VARCHAR(50) NOT NULL,
teamLeague VARCHAR(70),
yearFounded int(5) NOT NULL,
teamCrest VARCHAR(30),
leagueID int(3) NOT NULL,
PRIMARY KEY(teamID),
FOREIGN KEY(leagueID) REFERENCES league(leagueID)) ENGINE=innodb;
CREATE TABLE league (
leagueID int(3) AUTO_INCREMENT NOT NULL,
leagueName VARCHAR(50) NOT NULL,
leagueRegion VARCHAR(50) NOT NULL,
leagueSize int(3) NOT NULL,
leagueLogo VARCHAR(50) NOT NULL,
yearStarted int(4) NOT NULL,
PRIMARY KEY(leagueID)) ENGINE=innodb;
CREATE TABLE users (
user_id int(3) AUTO_INCREMENT NOT NULL,
user_name VARCHAR(30) NOT NULL,
user_email VARCHAR(70) NOT NULL,
user_pass VARCHAR(20) NOT NULL,
PRIMARY KEY (user_id)) ENGINE=innodb;
这是我在插入页面上的php代码。
<?php
include 'connect.php';
if(isset($_POST['button-submit'])){
$sql = "insert into player (playerName, playerAge, playerNation, playerPosition, playerTeam) values ('".$_POST['player_name']."', '".$_POST['player_age']."', '".$_POST['player_nation']."', '".$_POST['player_nation']."', '".$_POST['player_team']."')";
if(mysqli_query($con, $sql)){
header('Location:index.php');
}
else {
echo 'Error'.mysqli_error($con);
}
}
?>
<h2>Add player</h2>
<form action="" method="POST">
<label>Player Name: </label>
<input name="player_name">
<br>
<label>Player Age: </label>
<input name="player_age">
<br>
<label>Player Nation: </label>
<input name="player_nation">
<br>
<label>Player Position: </label>
<input name="player_position">
<br>
<label>Player Team: </label>
<input name="player_team">
<br>
<input type="submit" name="button-submit">
</form>
和我的索引页面显示数据
<?php
require "connect.php"
?>
<h2> Player Lists </h2>
<h3><a href="insertPlayer.php">Add player!</a></h3><br>
<table border="1" cellspacing="0" cellpadding="5px">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Nation</th>
<th>Position</th>
<th>Team</th>
<th>Team ID</th>
</tr>
<?php
$sql = 'select playerID, playerName, playerAge, playerNation, playerPosition, playerTeam, teamID from player';
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?=$row['playerID']?></td>
<td><?=$row['playerName']?></td>
<td><?=$row['playerAge']?></td>
<td><?=$row['playerNation']?></td>
<td><?=$row['playerPosition']?></td>
<td><?=$row['playerTeam']?></td>
<td><?=$row['teamID']?></td>
</tr>
<?php
}
}
?>
</table>
我很抱歉,如果这对你们大多数人来说都是非常基本的,但是我刚刚开始在大学里使用php和mysql就像2个月前一样,我的目标是如果是chelsea就有团队ID 1,如果是另一支球队,如果是另外一支球队,如果是切尔西等等,那么又是一支球队,等等。
更新
<?php
include 'connect.php';
$teamName = $_POST['player_team'];
$teamID = "select teamID from team WHERE teamName = $teamName";
if(isset($_POST['button-submit'])){
$sql = "insert into player (playerName, playerAge, playerNation, playerPosition, playerTeam, teamID) values ('".$_POST['player_name']."', '".$_POST['player_age']."', '".$_POST['player_nation']."', '".$_POST['player_nation']."', '".$_POST['player_team']."', '".$teamName."')";
if(mysqli_query($con, $sql)){
header('Location:index.php');
}
else {
echo 'Error'.mysqli_error($con);
}
}
?>
<h2>Add player</h2>
<form action="" method="POST">
<label>Player Name: </label>
<input name="player_name">
<br>
<label>Player Age: </label>
<input name="player_age">
<br>
<label>Player Nation: </label>
<input name="player_nation">
<br>
<label>Player Position: </label>
<input name="player_position">
<br>
<label>Player Team: </label>
<input name="player_team">
<br>
<input type="submit" name="button-submit">
</form>
好的,所以我更新了代码,现在我得到第4行的错误未定义索引,我无法理解
更新2
<?php
include 'connect.php';
if(isset($_POST['button-submit'])){
$teamName = $_POST['player_team'];
$teamID = "select teamID from team WHERE teamName = ".$_POST['player_team'];
$sql = "insert into player (playerName, playerAge, playerNation, playerPosition, playerTeam, teamID) values ('".$_POST['player_name']."', '".$_POST['player_age']."', '".$_POST['player_nation']."', '".$_POST['player_position']."', '".$_POST['player_team']."', '".$teamID."')";
if(mysqli_query($con, $sql)){
header('Location:index.php');
}
else {
echo 'Error'.mysqli_error($con);
}
}
?>
<h2>Add player</h2>
<form action="" method="POST">
<label>Player Name: </label>
<input name="player_name">
<br>
<label>Player Age: </label>
<input name="player_age">
<br>
<label>Player Nation: </label>
<input name="player_nation">
<br>
<label>Player Position: </label>
<input name="player_position">
<br>
<label>Player Team: </label>
<input name="player_team">
<br>
<input type="submit" name="button-submit">
</form>
更新3
<?php
include 'connect.php';
if(isset($_POST['button-submit'])){
$teamName = $_POST['player_team'];
$stmt = "SELECT teamID FROM team WHERE teamName = '".$teamName."'";
$teamID = mysqli_query($con, $stmt);
$sql = "INSERT INTO player (playerName, playerAge, playerNation, playerPosition, teamID) values ('".$_POST['player_name']."', '".$_POST['player_age']."', '".$_POST['player_nation']."', '".$_POST['player_position']."', $teamID)";
if(mysqli_query($con, $sql)){
header('Location:index.php');
}
else {
echo 'Error'.mysqli_error($con);
}
}
?>
答案 0 :(得分:0)
当您尝试更新或删除外键时会出现此消息,这是一种很好的安全措施。 所以要防止它试试这个
ON UPDATE CASCADE ON DELETE CASCADE
之后定义你的外键
FOREIGN KEY(teamID) REFERENCES team(teamID) ON UPDATE CASCADE ON DELETE CASCADE
也适用于其他相关表格。