繁体   English   中英

在一个提交上更新多个表将 id 从第一个表传递到第二个表

[英]Updating multiple tables on one submit passing id from first table to second

我试图通过一个表单更新 2 个表,表单提交到部件表,然后从部件插入记录中获取 id 并在 job_parts 表中创建一个条目。

我已经尝试了来自各种论坛的几个选项,但到目前为止没有任何运气,我的代码和数据库结构一起在下面。

更新:按照建议编辑代码,但只将数据发布到“parts”表而不是“job_parts 表”

表:!( https://drive.google.com/file/d/11I9HZrjc834_Ft5rqZoa0uFoJyDmMWGL/view?usp=sharing

if(isset($_POST['submitpart']))
{

    $job_id = $_POST['job_id'];
    $partName = $_POST['partName'];
    $partCost = $_POST['partCost'];
    $partRetail = $_POST['partRetail'];
    $partQuantity = $_POST['partQuantity'];

    $sql1 = "INSERT INTO parts (part_name, part_cost, part_rrp) VALUES ('$partName', '$partCost', '$partRetail');";
    $sql1 .= "SET @last_id_parts = LAST_INSERT_ID();";
    $sql1 .= "INSERT INTO job_parts (job_id, part_id, quantity) VALUES ('$job_id', @last_id_parts, '$partQuantity')";


    $outcome = mysqli_multi_query($conn, $sql1);
    if ($outcome) {
        do {
            // grab the result of the next query
            if (($outcome = mysqli_store_result($mysqli)) === false && 
 mysqli_error($mysqli) != '') {
                echo "Query failed: " . mysqli_error($mysqli);
            }
        } while (mysqli_more_results($mysqli) && 
 mysqli_next_result($mysqli)); // while there are more results
    } else {
        echo "First query failed..." . mysqli_error($mysqli);
    }

}

这种方法应该可以正常工作

    $sql1 = "INSERT INTO parts (part_name, part_cost, part_rrp) VALUES ('$partName', '$partCost', '$partRetail')";
    $result1=mysqli_query($con,$sql1);// where $con is connection string
    $last_id = mysqli_insert_id($con);// where $con is connection string
    $sql2 = "INSERT INTO job_parts (job_id, part_id, quantity) VALUES ('$job_id', $last_id , '$partQuantity')";
  $result2=mysqli_query($con,$sql2);// where $con is connection string

尝试添加; 在每个语句之后。

$sql1 = "INSERT INTO parts (part_name, part_cost, part_rrp) VALUES ('$partName', '$partCost', '$partRetail');
    SET @last_id_parts = LAST_INSERT_ID();
    INSERT INTO job_parts (job_id, part_id, quantity) VALUES ('$job_id', @last_id_parts, '$partQuantity');";

并将mysqli_query()更改为mysqli_multi_query()

我大概是这样写的。 (“可能”因为我很少使用mysqli

$conn->begin_transaction();

$stmt1 = $conn->prepare("INSERT INTO parts (part_name, part_cost, part_rrp) VALUES (?, ?, ?");
$stmt1->bind_param("sss", $partName, $partCost, $partRetail);
$stmt1->execute();

$stmt2 = $conn->prepare("INSERT INTO job_parts (job_id, part_id, quantity) VALUES (?, ?, ?)");
$stmt2->bind_param("sis", $job_id, $conn->insert_id, $partQuantity);
$stmt2->execute();

$conn->commit();

请注意,对于数字,您可以将参数与i绑定为整数或d为双(浮点数)而不是s为字符串。 例如,如果$job_id是一个整数,你会写:

$stmt2->bind_param("iis", $job_id, $conn->insert_id, $partQuantity);

关于错误处理,我建议阅读这篇文章: how-to-get-mysqli-error-information-in-different-environments

另请注意,参数化准备好的语句不仅有利于安全原因。 如果您尝试以自己的方式插入像“O'Conner”这样的有效名称( ... VALUES(.., '$lastName', ..) ),您的查询将失败。

以下工作完美

if(isset($_POST['submitpart']))
{

    $job_id = $_GET['id'];
    $partName = $_POST['partName'];
    $partCost = $_POST['partCost'];
    $partRetail = $_POST['partRetail'];
    $partQuantity = $_POST['partQuantity'];

     $sql1 = "INSERT INTO parts (part_name, part_cost, part_rrp) VALUES ('$partName', '$partCost', '$partRetail')";
$result1=mysqli_query($conn,$sql1);// where $con is connection string
$last_id = mysqli_insert_id($conn);// where $con is connection string
$sql2 = "INSERT INTO job_parts (job_id, part_id, quantity) VALUES ('$job_id', $last_id , '$partQuantity')";

$result2=mysqli_query($conn,$sql2);// 其中 $con 是连接字符串

}

作为以下的一部分

<script>
        function openForm() {
          document.getElementById("myForm").style.display = "block";
        }

        function closeForm() {
          document.getElementById("myForm").style.display = "none";
        }
        </script>


        <button class="open-button" onclick="openForm()">Add Part</button>

        <div class="form-popup" id="myForm">
          <form action="" class="form-container" method="POST">
            <h3>Add part</h3>



            <div class="divTable">
                <div class="divTableBody">
                    <div class="divTableRow">
                        <div class="divTableCell"><label for="name"><strong>Name/Description</strong></label></div>
                        <div class="divTableCell"><input id="partName" name="partName" required="" type="text" placeholder="Enter description" /></div>
                    </div>
                    <div class="divTableRow">
                        <div class="divTableCell"><label for="cost"><strong>Cost Price </strong></label></div>
                        <div class="divTableCell"><input id="partCost" name="partCost" required="" type="text" placeholder="Enter cost price" /></div>
                    </div>
                    <div class="divTableRow">
                        <div class="divTableCell"><label for="retail"><strong>Retail Price </strong></label></div>
                        <div class="divTableCell"><input id="partRetail" name="partRetail" required="" type="text" placeholder="Enter retail price" /></div>
                    </div>
                    <div class="divTableRow">
                        <div class="divTableCell"><label for="quantity"><strong>Quantity </strong></label></div>
                        <div class="divTableCell"><input id="partQuantity" name="partQuantity" required="" type="text" placeholder="Enter quantity" /></div>
                    </div>
                    <div class="divTableRow">
                        <div class="divTableCell"><button type="submitpart" name="submitpart" class="btn" onClick="alert('Added!')">Add</button></div>
                        <div class="divTableCell"> <button type="button" class="btn cancel" onclick="closeForm()">Close</button></div>
                    </div>
                </div>
            </div>


          </form>
        </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM