gpt4 book ai didi

php - 准备 SQL 语句并仅在不重复时输入

转载 作者:行者123 更新时间:2023-11-29 17:53:39 31 4
gpt4 key购买 nike

我正在为我工​​作的小型企业创建销售订单表单。我有一个表单连接到数据库以获取下拉菜单,然后连接到文本框以在需要时添加新数据。如果前面的部分中的所有数据框均已填写,还会出现另外四个部分。

链接到数据库的输入包括CompanyCustomerToolPart然后是用于输入要完成的工作的Description。因此,一旦填写了 ToolPartDescription,第二部分将包含另一个 Tool部分描述进入 View 。我有一个名为 Update.php 的脚本,它确定已填充哪些字段以及将哪些内容输入到数据库中。我一直在努力做的是避免重复条目。因此,如果在第一部分输入了 UpdateNewTool,然后在第二部分再次放入该字段以将另一个 Part 与其关联,则将插入该数据两次。我怎样才能扔掉这些输入语句之一?

我的update.php代码:

<?php

$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "Sales_Orders";

$UpdateCompany=$_POST['UpdateCompany'];
$UpdateNewCompany=$_POST['UpdateNewCompany'];

$UpdateCustomer=$_POST['UpdateCustomer'];
$UpdateNewCustomer=$_POST['UpdateNewCustomer'];

$UpdateTool=$_POST['UpdateTool'];
$UpdateTool2=$_POST['UpdateTool2'];
$UpdateTool3=$_POST['UpdateTool3'];
$UpdateTool4=$_POST['UpdateTool4'];

$UpdateNewTool=$_POST['UpdateNewTool'];
$UpdateNewTool2=$_POST['UpdateNewTool2'];
$UpdateNewTool3=$_POST['UpdateNewTool3'];
$UpdateNewTool4=$_POST['UpdateNewTool4'];

$UpdateNewPart=$_POST['UpdateNewPart'];
$UpdateNewPart2=$_POST['UpdateNewPart2'];
$UpdateNewPart3=$_POST['UpdateNewPart3'];
$UpdateNewPart4=$_POST['UpdateNewPart4'];

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$data = [];


// BEGIN CONDITIONAL STATEMENTS



// New Company added by itself
if (!empty($UpdateNewCompany)) {
$data[] = [ '1', $UpdateNewCompany, $UpdateNewCompany, NULL ];
$data[] = [ '2', '', '', $UpdateNewCompany ];
$data[] = [ '3', '', '', $UpdateNewCompany ];
}

//Using the Company Drop-down box
if (!empty($UpdateCompany)) {
$UpdateNewCompany = $UpdateCompany; // If there is no new company inserted, $UpdateNewCompany becomes $UpdateCompany

if (!empty($UpdateNewCustomer)) {
$data[] = [ '2', $UpdateNewCustomer, $UpdateNewCustomer, $UpdateNewCompany ];
}



if (empty($UpdateTool)) { //If there is a new tool to add

if (!empty($UpdateNewTool)) {
$data[] = [ '3', $UpdateNewTool, $UpdateNewTool, $UpdateNewCompany ];
$data[] = [ '4', '', '', $UpdateNewTool ];
}
if (!empty($UpdateNewPart)) {
$data[] = [ '4', $UpdateNewPart, $UpdateNewPart, $UpdateNewTool ];
}
}

if (!empty($UpdateTool)) { // If there is no new tool to add
$UpdateNewTool = $UpdateTool;

if (!empty($UpdateNewPart)) {
$data[] = [ '4', $UpdateNewPart, $UpdateNewPart, $UpdateNewTool ];
}
}

//Part 2
if (empty($UpdateTool2)) { //If there is a new tool to add

if (!empty($UpdateNewTool2)) {
$data[] = [ '3', $UpdateNewTool2, $UpdateNewTool2, $UpdateNewCompany ];
$data[] = [ '4', '', '', $UpdateNewTool2 ];
}
if (!empty($UpdateNewPart2)) {
$data[] = [ '4', $UpdateNewPart2, $UpdateNewPart2, $UpdateNewTool2 ];
}
}

if (!empty($UpdateTool2)) { // If there is no new tool to add
$UpdateNewTool2 = $UpdateTool2;

if (!empty($UpdateNewPart2)) {
$data[] = [ '4', $UpdateNewPart2, $UpdateNewPart2, $UpdateNewTool2 ];
}
}

//Part 3
if (empty($UpdateTool3)) { //If there is a new tool to add

if (!empty($UpdateNewTool3)) {
$data[] = [ '3', $UpdateNewTool3, $UpdateNewTool3, $UpdateNewCompany ];
$data[] = [ '4', '', '', $UpdateNewTool3 ];
}
if (!empty($UpdateNewPart3)) {
$data[] = [ '4', $UpdateNewPart3, $UpdateNewPart3, $UpdateNewTool3 ];
}
}

if (!empty($UpdateTool3)) { // If there is no new tool to add
$UpdateNewTool3 = $UpdateTool3;

if (!empty($UpdateNewPart3)) {
$data[] = [ '4', $UpdateNewPart3, $UpdateNewPart3, $UpdateNewTool3 ];
}
}

//Part 4
if (empty($UpdateTool4)) { //If there is a new tool to add

if (!empty($UpdateNewTool4)) {
$data[] = [ '3', $UpdateNewTool4, $UpdateNewTool4, $UpdateNewCompany ];
$data[] = [ '4', '', '', $UpdateNewTool4 ];
}
if (!empty($UpdateNewPart4)) {
$data[] = [ '4', $UpdateNewPart4, $UpdateNewPart4, $UpdateNewTool4 ];
}
}

if (!empty($UpdateTool4)) { // If there is no new tool to add
$UpdateNewTool4 = $UpdateTool4;

if (!empty($UpdateNewPart4)) {
$data[] = [ '4', $UpdateNewPart4, $UpdateNewPart4, $UpdateNewTool4 ];
}
}
}

$insert = $conn->prepare("INSERT INTO Sales_Orders_dynlist_items (listid,name,value,parent) VALUES (?, ?, ?,?)");
if ( $insert === false ) {
echo "Error:".$conn->error;
die;
}

foreach ( $data as $item ){
if ( $insert->bind_param("ssss", ...$item) === false ) {
echo "Error:".$conn->error;
}
if ( $insert->execute() === false ) {
echo "Error:".$conn->error;
}
}




$conn->close();
?>

我很确定需要工作的地方是:

foreach ( $data as $item ){
if ( $insert->bind_param("ssss", ...$item) === false ) {
echo "Error:".$conn->error;
}
if ( $insert->execute() === false ) {
echo "Error:".$conn->error;
}
}

例如,有时我们需要具有相同工具编号的不同零件。因此,如果用户输入新的作业编号(例如“1234”)和“abcd”部分,则输入有关所做操作的描述。然后在下一部分中,“1234”尚未出现在保管箱中,因为它尚未添加到数据库中。因此,用户再次将“1234”放入工具中,但将“dcba”作为部分和另一个描述。提交后,数据库中将有两个条目 ['3', '1234', '1234', 'Whatever Company'];

感谢您的任何意见!

最佳答案

所以我决定采取不同的方法。我最终做的是设置一个 javascript 脚本,用于确定 NewTool 输入框是否为空,如果不是,则将该输入值作为选项添加到 Tool2 等框中。 JavaScript 如下:

function addOpTool() {
if(document.getElementById('NewTool').value != '') {
var x = document.getElementById("Tool2");
var y = document.getElementById("Tool3");
var z = document.getElementById("Tool4");
var test = document.getElementById("NewTool");
var option = document.createElement("option");
var option2 = document.createElement("option");
var option3 = document.createElement("option");
option.text = test.value;
option2.text = test.value;
option3.text = test.value;
x.add(option);
y.add(option2);
z.add(option3);
}
}

function addOpTool2() {
if(document.getElementById('NewTool2').value != '') {
var y = document.getElementById("Tool3");
var z = document.getElementById("Tool4");
var test = document.getElementById("NewTool2");
var option2 = document.createElement("option");
var option3 = document.createElement("option");
option2.text = test.value;
option3.text = test.value;
y.add(option2);
z.add(option3);
}
}

function addOpTool3() {
if(document.getElementById('NewTool3').value != '') {
var z = document.getElementById("Tool4");
var test = document.getElementById("NewTool3");
var option3 = document.createElement("option");
option3.text = test.value;
z.add(option3);
}
}

我将这些函数添加到 NewToolNewTool2NewTool3 的相应 onchange 中。

例如,用户在 NewTool 中输入“1234”,在 NewPart 中输入“LH-Side”。然后在下面的表单部分中,Tool2 下拉框中现在有一个选项“1234”,用户只需选择该选项,然后在 NewPart2 中输入“RH-Side” >.

提交表单时,只会将工具#“1234”的一个实例放入数据库,解决了我遇到的问题!

关于php - 准备 SQL 语句并仅在不重复时输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49072007/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com