Reputation: 163
I found some information about this subject (Trying to insert into mysql database ignoring blank fields). But then I can get some issues with injections...
I already added NOT NULL
to my columns but it still inserted a value with ''
. So it didn't give me the result I wanted.
Anyways I got this now:
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!";
}
}
}
My pageis for creating a schedule. But sometimes you only want to add 1 task. So it could be possible that $Date2
, $productDescription2
and the others are empty. But it still adds the row to my mysql database with just values 0000-00-00 '' and 0, 0, 0.
I can easily stop this by making an IF statement checking if these fields are set. If not, it uses a different query.
But I got like 5 rows for adding information to the database ($Date3, $Date4 and $Date5 + all the other information per row).
So when I will solve this with if statements I get huge if statements...
Is there a easier way to solve this issue? Something I can tell my database for not adding a row when they are empty?
Upvotes: 0
Views: 680
Reputation: 1617
Design your database such that you store information regarding Date and product description. Use something like:
Post the data using arrays and than iterate the array and insert one by one
Upvotes: 1