dongying195959 2017-02-28 16:12
浏览 20

使用外键从One Form MySQLi发布到两个表

So, I have one form and I'm trying to insert data (via POST) to two tables.
There is a foreign key linking both tables by AUTO_INCREMENT ID.

I've searched the site extensively and tried all the suggested (for those issues) and none worked.

I'd assume this is because their problem was similar, but not exact to mine.

Here's my 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>";
  • 写回答

1条回答 默认 最新

  • duanping3587 2017-02-28 16:19
    关注

    You haven't told us the error message or described exactly your database schema, but here's a guess:

    Use mysqli_insert_id() after the first insert so that you can supply the resulting id (returned by mysqli_insert_id()) to the following INSERT statement.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)