我试图通过一种表单更新2个表,该表单提交到部件表,然后从部件插入记录中获取ID,并在job_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);
}
}
答案 0 :(得分:1)
此方法应该可以正常工作
$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
答案 1 :(得分:0)
尝试在每个语句后添加;
。
$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()
。
答案 2 :(得分:0)
这可能就是我写的方式。 (“可能是因为我很少使用 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
还请注意,带参数的预准备语句不仅出于安全原因也是不错的。如果您尝试以自己的方式(... VALUES(.., '$lastName', ..)
插入有效名称,例如“ O'Conner”,则查询将失败。
答案 3 :(得分:0)
以下内容效果很好
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>