【问题标题】:PHP Cannot Add Or Update Child Field - Foreign Key ErrorPHP 无法添加或更新子字段 - 外键错误
【发布时间】:2012-05-29 01:53:55
【问题描述】:

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

无法添加或更新子行:外键约束失败 (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>

【问题讨论】:

    标签: php html mysql


    【解决方案1】:

    您必须将EventID 插入到您的results 记录中:

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

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

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

    【讨论】:

    • 感谢您的回复!所以如果我把 if (isset($_POST['submit'])) { $sql="INSERT INTO results (EventID, MemberID, Score, Place) VALUES ('".$_POST['EventID']."', '".$_POST['student']."', '".$_POST['Score']."', '".$_POST['Place']."')";这会正确地从 URL 中读取 EventID 吗?
    • 即使使用值为 $_POST['EventID'] 的 EventID 添加到我的查询中,我仍然收到错误消息。
    • @user1371500:您的$id 变量中已经有一个转义 版本的事件ID,因此您可以使用它。你也应该,至少,类似地逃避你的其他变量;但请 阅读并记下我在上面提出的观点。
    • @user1371500:因为这是家庭作业,您应该包含homework 标签——我冒昧地为您添加了它。关于您的错误,一定是您使用的 ID 在 events 表中不存在。您必须先在那里创建它。
    • @user1371500:在尝试INSERT INTO results ...之前,您必须先INSERT INTO events ...(或者使用现有事件的ID,以适当的为准)。
    猜你喜欢
    • 1970-01-01
    • 2012-12-22
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2014-05-10
    • 2022-12-10
    相关资源
    最近更新 更多