duanjurong1347 2017-07-20 05:49
浏览 46
已采纳

MYSQL和PHP:数据未插入

I a trying to add data into these two foreign key fields but it is causing an error. The thing is, When i go on MYSQL and try inserting data, it actually does work, however, when i try the same with php, I get the following error:

Notice: Undefined variable: CantactId in C:\xamppp\htdocs\MDK\profiles.php on line 14
Error updating database: Cannot add or update a child row: a foreign key constraint fails (`companies`.`company`, CONSTRAINT `company_ck` FOREIGN KEY (`CantactId`) REFERENCES `contacts` (`ContactId`) ON DELETE NO ACTION ON UPDATE CASCADE)

this error only exists between the foreign key fields of my table, not the rest. What exactly am i doing wrong? Here is my php code; If you need to check something else too, please let me know as i really am clueless about how to use this website and ask my question.

<?php
include('database1.php');

if(isset($_POST['insert']))
{
    $CompanyCode=$_POST['CompanyCode'];
    $CompanyName = $_POST['CompanyName'];
    $Address = $_POST['Address'];
    $Fax = $_POST['Fax'];
    $Website=$_POST['Website'];
    $Telephone = $_POST['Telephone'];
    $ContactId = $_POST['CantactId'];
    $CountryCode=$_POST['CountryCode'];
     $sql = "INSERT INTO `company`(`CompanyCode`, `CompanyName`, `Address`, `Fax`, `Website`, `Telephone`, `CantactId`, `CountryCode`) VALUES ('$CompanyCode','$CompanyName','$Address','$Fax','$Website','$Telephone','$CantactId','$CountryCode')";
     $query=mysql_query($sql) or die ('Error updating database: '.mysql_error());;
   if($query==TRUE)
   {
    header('Refresh:0; details1.php');
   }
   else
       echo "Sorry";
}

?>
  <form action="" method="post">
   Company Code: <input type="text" name="CompanyCode" ><br><br>
   Company Name: <input type="text" name="CompanyName"><br><br>
   Address: <input type="text" name="Address" ><br><br>
   Fax: <input type="number" name="Fax"  ><br><br>
   Website: <input type="text" name="Website"  ><br><br>
    Telephone: <input type="text" name="Telephone"  ><br><br>
   ContactId: <input type="number" name="CantactId"  ><br><br>
   Country Code: <input type="number" name="CountryCode"><br><br>
    <input type="submit" name="insert" value="Insert">
    </form>

</form>
  • 写回答

1条回答 默认 最新

  • douhu4091 2017-07-20 05:57
    关注

    You have typo error here. Change $CantactId to $ContactId

    $sql = "INSERT INTO `company`(`CompanyCode`, `CompanyName`, `Address`, `Fax`, `Website`, `Telephone`, `CantactId`, `CountryCode`) VALUES ('$CompanyCode','$CompanyName','$Address','$Fax','$Website','$Telephone','$ContactId','$CountryCode')";
    

    Also dont use mysql_ It is deprecated and completely removed in PHP 7. Use mysqli_ or PDO instead

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题