PHP 无法添加或更新子字段 - 外键错误

标签 php html mysql

尝试将某些值插入 MySQL 数据库时出现错误 - 我的页面当前读取通过 URL 传递的值“EventID”,并允许我根据该 EventID 添加结果。我目前有一个下拉框,由成员表中的成员填充。
我收到这个可怕的错误:

Cannot add or update a child row: a foreign key constraint fails (clubresults.results, CONSTRAINT ResultEvent FOREIGN KEY (EventID) REFERENCES events (EventID) ON DELETE CASCADE)

我无法更改表结构,因此我们将不胜感激。 注意 - 我目前正在让它回显 SQL 以查找错误以及为什么它不会插入。

<?php

error_reporting (E_ALL ^ E_NOTICE); 
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("clubresults", $con);

   // Get id from URL
    $id = mysql_real_escape_string($_GET['EventID']);

    // If id is number
    if ($id > 0) 
    {
         // Get record from database
         $sql = "
            SELECT EventID
            FROM results 
            WHERE EventID = " . $id;
         $result = mysql_query($sql); 
         }
if (isset($_POST['submit'])) {       
  $sql="INSERT INTO results (MemberID, Score, Place)
VALUES
('".$_POST['student']."', '".$_POST['Score']."', '".$_POST['Place']."')";

$add_event = mysql_query($sql) or die(mysql_error());;

echo $add_event;
}

HTML 表单 -

$_SERVER['PHP_SELF']?>" method="post"> 
                        <table border="0"><p>
                        <tr><td colspan=2></td></tr>
                        <tr><td>Member Name: </td><td>
                        <?php
                        $query="SELECT * FROM members";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=student value=''>Student Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value='$nt[MemberID]'>$nt[Firstname] $nt[Surname]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
?>

                        <tr><td>Score:</td><td> 
                        <input type="text" name="Score" maxlength="10"> 
                        <tr><td>Place:</td><td> 
                        <input type="text" name="Place" maxlength="10"> 
                        </td></tr> 
                        <tr><th colspan=2><input type="submit" name="submit" 
                        value="Add Result"> </th></tr> </table>
                        </form>

最佳答案

您必须将EventID插入您的结果记录中:

$sql="INSERT INTO results (MemberID, Score, Place, EventID) VALUES (?, ?, ?, ?)";

请注意,我使用了 ? 占位符来代替您的 $_POST 变量(这使您容易受到 SQL injection 的攻击)。

您应该使用准备好的语句,将变量作为参数传递到其中,这些变量不会针对 SQL 进行评估,但它们在您正在使用的古老 MySQL 扩展中不可用(社区有 begun deprecating 无论如何,所以你真的应该停止用它编写新代码);使用改进的 MySQLi 来代替扩展名或 PDO抽象层。

关于PHP 无法添加或更新子字段 - 外键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10792264/

相关文章:

java - Spring:如何从JPA中的两个表中获取数据

php - 插入多个用户和随 secret 码 Php Mysql

php - 如何在 symfony 中使用 bcrypt 密码加密添加管理员?

html - 使菜单响应 - Flex

php - 启动页面时在areamap的特定位置绘制一个矩形

javascript - 如何使用 HTML 和 javascript 从表单框中显示 URL?

mysql - SQL 中 SOURCE 命令的正确语法是什么

MySql 错误 2002

php - DateInterval 不接受以毫秒为单位的 ISO 8601 时间段

php - imagestring() 函数中的 utf-8 字符