douba8048 2013-09-17 01:02
浏览 88

使用MySQL将多个表单中的数据插入多个表中

I have multiple forms that I am trying to insert data from into MySql. Each form has a different table associated with that information. I am using an insert.php file to insert the data. The first form works great. However when I add any other variables to the initial file it errors out. This is my insert file:

<?php # NAME OF PROGRAM GOES HERE

require_once 'config.php'; 

// Get values from form
$Fname        = $_POST['first_name'];
$email        = $_POST['email']; 

// Insert data into mysql
$sql="INSERT INTO entry (first_name, email)
VALUES ('$Fname', '$email')";
$result = mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful Entry";
}
else {
echo "ERROR";
}
mysql_close();
?> 

This is the first form:

<form action="insert.php" method="post" name="admin" onSubmit="return validateForm()">
<p>Please enter First Name <input type="text" name="first_name" required="required"/>
<p>Please enter your Email<input type="text" name="email" required="required" ><br />
<input type="submit" name="submit" value="Submit" />
</form>

The second form is:

<form action="insert.php" method="post" onSubmit="return validateForm()">
<p>Favorite Winery Name <input type="text" name="fav_winery" /> <br />
<p>Favorite White Wine<input type="type" name="fav_white" /> <br />
<p>Favorite Red Wine <input type="type" name="fav_red" /> <br /><br />
<input type="submit" name="submit" value="Submit" />
</form>

How do I INSERT the information from the second form into another table already created?

  • 写回答

4条回答 默认 最新

  • doutu2017 2013-09-17 01:08
    关注

    I would suggestion adding a hidden variable to each form for their action and then do a if statement in insert.php:

    if($_GET['action'] == 'insertName'){
    
    ##insert code and form validation goes here
    
    }elseif($_GET['action'] == 'insertFav'){
    
    ##insert code and form validation goes here
    
    }
    

    and add hidden inputs to the forms change ACTION-NAME to something to differentiate between forms:

    <input type="hidden" name="action" value="insert*ACTION-NAME*" />
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c