Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MYSQL和PHP:数据未插入_Php_Html_Mysql_Sql_Database - Fatal编程技术网

MYSQL和PHP:数据未插入

MYSQL和PHP:数据未插入,php,html,mysql,sql,database,Php,Html,Mysql,Sql,Database,我试图将数据添加到这两个外键字段中,但这会导致错误。问题是,当我在MYSQL上尝试插入数据时,它实际上是有效的,但是,当我在php上尝试同样的方法时,我得到了以下错误: 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 constr

我试图将数据添加到这两个外键字段中,但这会导致错误。问题是,当我在MYSQL上尝试插入数据时,它实际上是有效的,但是,当我在php上尝试同样的方法时,我得到了以下错误:

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)
此错误仅存在于我的表的外键字段之间,而不存在于其他字段之间。我到底做错了什么?这是我的php代码;如果您还需要检查其他内容,请让我知道,因为我对如何使用本网站真的一无所知,并询问我的问题

<?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>

你这里有打字错误。将$CantactId更改为$ContactId


也不要使用mysql,因为它在PHP7中已被弃用并完全删除。改用mysqli_uu或PDO

sql错误显示您试图输入的行中的任何唯一id都有空值它应该是值“$ContactId”,。。。不再使用值“$CantactId”…而是停止使用不推荐使用的mysql_*API。将mysqli_*或PDO与prepared语句一起使用,以防止SQL注入。我不是向下投票者。但我想这是因为如果这是一个打字错误的问题,你应该使用接近投票。
$sql = "INSERT INTO `company`(`CompanyCode`, `CompanyName`, `Address`, `Fax`, `Website`, `Telephone`, `CantactId`, `CountryCode`) VALUES ('$CompanyCode','$CompanyName','$Address','$Fax','$Website','$Telephone','$ContactId','$CountryCode')";