dtziv24262 2014-11-16 00:51
浏览 57
已采纳

PHP MySQL将信息从一种形式插入到多个表中

So I have form1 that contains information from multiple tables in a database. I've got listboxes and textboxes within this form that have that information. So all I'm trying to do is insert whatever information the user submits back into the database and have it outputted on form2. I've got my INSERT INTOs on my output page. I know you can't use one INSERT INTO query, so I was wondering how to use multiple INSERTS and submit that information back into the database.

The variables created below come from the previous page and all of the values are there.

if (isset($_POST['n_submit'])){
    $oid = $_POST['oid'];
    $odate = $_POST['odate'];
    $ostatus = $_POST['ostatus'];

    $cfname = $_POST['cfname'];
    $cname = $_POST['clname'];
    $efname = $_POST['efname'];
    $elname = $_POST['elname'];
    echo "New record created successfully";
    $db = mysqli_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password') or die ("I cannot connect to the database because: ".mysqli_connect_error());

    $query = "select status_id from ostatus where status_type = '$ostatus'";
    $result = mysqli_query($db, $query) or die("Error in SQL statement:" .mysqli_error());
    $row = mysqli_fetch_array($result);

    $statusid = $row[0];

    $query1 = "insert into cust ('c_fname', 'c_lname') values ('$cfname',    $clname)";
    $result1 = mysqli_query($db, $query1) or die("Error in SQL statement:" .mysqli_error());


    $query2 = "insert into employed  ('e_fname', e_lname) values ('$efname', '$elname')";
    $result2 = mysqli_query($db, $query1) or die("Error in SQL statement:" .mysqli_error());


    $query3 ="INSERT INTO sorder (o_id, o_date, s_id) VALUES ('{$oid}', '{$odate}', '{$statusid}')";
    $result3 = mysqli_query($db, $query3);
}
  • 写回答

1条回答 默认 最新

  • dongtao4319 2014-11-16 00:59
    关注

    First of all your query is vulnerable to SQL injection. I am not going to fix that.

    Second, you should Google how to handle forms properly. And you should consider starting SQL transaction if you really care about the data to go into all the tables for sure.

    Third, you should be able to use multiple inserts like you are doing in your code. but you need to correct your syntax errors.

    Try this code (I also removed the select code are based on your question it is not needed)

    if (isset($_POST['n_submit'])){
        $oid = $_POST['oid'];
        $odate = $_POST['odate'];
        $ostatus = $_POST['ostatus'];
        $cfname = $_POST['cfname'];
        $cname = $_POST['clname'];
        $efname = $_POST['efname'];
        $elname = $_POST['elname'];
    
        $db = mysqli_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password') or die ("I cannot connect to the database because: ".mysqli_connect_error());
    
    
        $query1 = "insert into cust (c_fname, c_lname) values ('".$cfname."',    '".$clname."')";
        $result1 = mysqli_query($db, $query1) or die("Error in SQL statement:" .mysqli_error());
    
    
        $query2 = "insert into employed  (e_fname, e_lname) values ('".$efname."', '".$elname."')";
        $result2 = mysqli_query($db, $query2) or die("Error in SQL statement:" .mysqli_error());
    
    
        $query3 ="INSERT INTO sorder (o_id, o_date, s_id) VALUES ('".$oid."', '".$odate."', '".$statusid."')";
        $result3 = mysqli_query($db, $query3);
    
      if($result1 && $result2 && $result3)
         echo 'New record created successfully';
      else
         echo 'something did not work';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题