gpt4 book ai didi

php - MYSQL INSERT 查询不使用空字段

转载 作者:行者123 更新时间:2023-11-29 11:46:56 24 4
gpt4 key购买 nike

我找到了一些有关此主题的信息 ( Trying to insert into mysql database ignoring blank fields )。但随后我可能会遇到一些注入(inject)问题......

我已经将 NOT NULL 添加到我的列中,但它仍然插入了一个带有 '' 的值。所以它没有给我我想要的结果。

无论如何,我现在得到了这个:

if(isset($_POST['btnOpslaan']))
{
include_once("dbConnect.php");

$Date1 = strip_tags($_POST['Datum1']);
$ProductDescription1 = strip_tags($_POST['ProductOmschrijving1']);
$Amount1 = strip_tags($_POST['Aantal1']);
$AmountCreated1 = strip_tags($_POST['AantalGemaakt1']);
$Difference1 = strip_tags($_POST['Verschil1']);

$Date2 = strip_tags($_POST['Datum2']);
$ProductDescription2 = strip_tags($_POST['ProductOmschrijving2']);
$Amount2 = strip_tags($_POST['Aantal2']);
$AmountCreated2 = strip_tags($_POST['AantalGemaakt2']);
$Difference2 = strip_tags($_POST['Verschil2']);

$Query = "INSERT INTO productielijn1 (Datum, Productomschrijving, Aantal, AantalGemaakt, Verschil) VALUES(?, ?, ?, ?, ?), (?, ?, ?, ?, ?);

if($stmt = $dbCon->prepare($Query))
{
$stmt->bind_param('ssiiissiii',
$Date1, $ProductDescription1, $Amount1, $AmountCreated1, $Difference1,
$Date2, $ProductDescription2, $Amount2, $AmountCreated2, $Difference2);

if( $stmt->execute() === TRUE)
{
$Message = "Planning is aangemaakt!";
}
}
}

我的页面用于创建时间表。但有时您只想添加 1 个任务。因此,$Date2$productDescription2 和其他内容可能为空。但它仍然将该行添加到我的 mysql 数据库中,仅包含值 0000-00-00 '' 和 0, 0, 0。

我可以通过使用 IF 语句检查这些字段是否已设置来轻松阻止此情况。如果不是,它将使用不同的查询。

但是我有大约 5 行用于向数据库添加信息($Date3、$Date4 和 $Date5 + 每行的所有其他信息)。

所以当我用 if 语句解决这个问题时,我会得到巨大的 if 语句......

有没有更简单的方法来解决这个问题?我可以告诉我的数据库,当它们为空时不添加行吗?

最佳答案

设计您的数据库,以便存储有关日期和产品描述的信息。使用类似的东西:

时间表_id|数据|产品描述 |安塔尔 | AantalGemaakt |

1 | XYZ| XYZ|自闭症谱系障碍 |萨达萨 |

1 | ABC | dsf|斧头 | asddsa |

使用数组发送数据,然后迭代数组并逐一插入

关于php - MYSQL INSERT 查询不使用空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34761217/

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