douzuo0711 2015-12-08 20:30
浏览 89

参照完整性错误

I am having a bit of trouble with referential integrity errors on my database final exam project. I have tried to figure it out myself, but to no avail, and I am hoping someone here can point me in the right direction.

The problem is that I have three tables with the first two being parent tables to the third table. I am trying to construct a php script that will update the two parent tables and then insert the data into the child table. What I have thus far, I thought would work, but whenever I try to use it, I get the following error:

INSERT INTO Purchases (CustomerID, PurchaseOrderNo, PurchaseTotal, DateOfPurchase, SalesPersonID,SpecialOrder) VALUES ('10', '0000','100.00','0000-00-00','5555','N')

Cannot add or update a child row: a foreign key constraint fails ('xxxxxx'.'Purchases', CONSTRAINT 'Purchases_ibfk_1' FOREIGN KEY ('CustomerID') REFERENCES 'CustomerInfo'('CustomerID') ON DELETE CASCADE ON UPDATE CASCADE)'

Any thoughts where I have gone wrong?

        ########## FOREIGN KEY CHECK START ##########
$sql = "select count(*) as count from CustomerInfo where '$customerid' = CustomerID";
$result = mysqli_query($con,$sql)
or  die('Error: ' . mysql_error());

$row = mysqli_fetch_assoc($result);
    if ( $row['count']==0 ){
    "INSERT INTO CustomerInfo ('CustomerID') VALUES ('$customerid');";
    echo "<p>Customer ID Not Found. <br />New CustomerID Created.</p>";
   }

$sql2 = "select count(*) as count from EmployeeInfo where '$salespersonid' =  SalesPersonID;";
$result2 = mysqli_query($con,$sql)
 or  die('Error: ' . mysql_error());
 $row2 = mysqli_fetch_assoc($result2);

if ( $row2['count']==0 ){
"INSERT INTO EmployeeInfo ('SalesPersonID')VALUES ('$salespersonid');";
echo "<p>Salesperson ID Not Found. <br />New Salesperson ID Created.</p>";
}

    ########## FOREIGN KEY CHECK END ##########

    ########## DATA ENTRY SQL STATEMENT START ##########
$sql3 = "INSERT INTO Purchases (CustomerID, 
          PurchaseOrderNo, 
          PurchaseTotal, 
          DateOfPurchase, 
          SalesPersonID,
          SpecialOrder) 
VALUES ('$customerid',   
        '$purchaseorderno',
        '$purchasetotal',
        '$dateofpurchase',
        '$salespersonid',
        '$specialorder')";
    ########### DATA ENTRY SQL STATEMENT END ##########

    ########## INPUT SUCCESS/FAILURE REPORTING#########
if (mysqli_query($con, $sql3)) {
    echo "<P>Record Successfully Created</P><BR />";
} else {
  echo "Error: " . $sql9. "<br>" . mysqli_error($con);
}
mysqli_close($con);
echo "<P>Connection Successfully Closed.</P>";
  • 写回答

1条回答 默认 最新

  • doushang4274 2015-12-08 20:59
    关注

    You aren't executing your insert queries for CustomerInfo or EmployeeInfo

        $sql = "select count(*) as count from CustomerInfo where '$customerid' = CustomerID";
        $result = mysqli_query($con,$sql)
        or  die('Error: ' . mysql_error());
    
        $row = mysqli_fetch_assoc($result);
            if ( $row['count']==0 ){
    
        ###THE NEXT LINE DOESN'T DO ANYTHING###
            "INSERT INTO CustomerInfo ('CustomerID') VALUES ('$customerid');";
            echo "<p>Customer ID Not Found. <br />New CustomerID Created.</p>";
           }
    

    Also you don't want to put single quotes around the column name in your insert statements so instead of:

    INSERT INTO CustomerInfo ('CustomerID') VALUES ('$customerid')
    

    you want:

    INSERT INTO CustomerInfo (CustomerID) VALUES ('$customerid')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看