所以,我有一个表单,我正在尝试将数据(通过POST)插入到两个表中。
有一个外键通过AUTO_INCREMENT ID
链接两个表。
我已经广泛搜索了该网站,并尝试了所有建议(针对这些问题)并且没有工作。
我认为这是因为他们的问题很相似,但与我的问题不一样。
这是我的PHP :
// Escape user inputs for security
$companyName = mysqli_real_escape_string($con, $_REQUEST['companyName']);
$companyAddress =mysqli_real_escape_string($con,$_REQUEST['companyAddress']);
$companyCity = mysqli_real_escape_string($con, $_REQUEST['companyCity']);
$companyState = mysqli_real_escape_string($con, $_REQUEST['companyState']);
$companyZip = mysqli_real_escape_string($con, $_REQUEST['companyZip']);
$companyPhone = mysqli_real_escape_string($con, $_REQUEST['companyPhone']);
$companyURL = mysqli_real_escape_string($con, $_REQUEST['companyURL']);
$contactName = mysqli_real_escape_string($con, $_REQUEST['contactName']);
$contactEmail = mysqli_real_escape_string($con, $_REQUEST['contactEmail']);
$contactPosition = mysqli_real_escape_string($con,$_REQUEST['contactPosition']);
// Attempt insert query execution
$sql1 = "INSERT INTO companyData (companyName, companyAddress, companyCity, companyState, companyZip, companyPhone, companyURL)
VALUES ('$companyName', '$companyAddress', '$companyCity', '$companyState', '$companyZip', '$companyPhone', '$companyURL')";
$sql2 = "INSERT INTO contactData (contactName, contactEmail, contactPhone, contactPosition)
VALUES ('$contactName', '$contactEmail', '$contactPhone', '$contactPosition')";
if (!mysqli_query($con,$sql1))
{
die('Error: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql2))
{
die('Error: ' . mysqli_error($con));
}
echo "<center><h1>Record updated successfully.<br>
Go <a href='data/form_companyinfo.php'>Here </a>Next</h1></center>";
答案 0 :(得分:0)
您没有告诉我们错误消息或描述了您的数据库架构,但这是一个猜测:
在第一次插入后使用mysqli_insert_id()
,以便您可以将结果id(由mysqli_insert_id()
返回)提供给以下INSERT语句。