mardi 13 février 2018

How to enter SQL data only if input is not empty

I am creating a Purchase Orders form/database for a small tool and die shop I work for. I have a form setup with drop down boxes for Company, Customer Name, Tool, and Part. Alongside these dropdowns are text boxes that the user can input new data into the database. Currently I am using a PHP file with about 13 if statements to determine what input boxes are empty and where data needs to be inserted.

Here is part of my PHP file with just 2 of the if statements:

<?php

$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "Purchase_Orders";

$NewCompany=$_POST['NewCompany'];
$NewCustomer=$_POST['NewCustomer'];
$NewTool=$_POST['NewTool'];
$NewPart=$_POST['NewPart'];

$Company=$_POST['Company'];
$Customer=$_POST['Customer'];
$Tool=$_POST['Tool'];



// NewCompany added by itself with NewCustomer, NewTool, and NewPart empty
if (!empty($NewCompany) && empty($NewCustomer) && empty($NewTool) && empty($NewPart)) {

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}        

$sql = "INSERT INTO Purchase_Orders_dynlist_items (listid,name,value,parent) VALUES ('1', '$NewCompany', '$NewCompany',NULL), ('2', '', '', '$NewCompany'), ('3', '', '', '$NewCompany')";

if ($conn->query($sql) === TRUE) {
    header( 'Location: index.html');
    exit;
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}


// NewCompany and NewCustomer added with NewTool and NewPart empty
if (!empty($NewCompany) && !empty($NewCustomer) && empty($NewTool) && empty($NewPart)) {

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}        

$sql = "INSERT INTO Sales_Orders_dynlist_items (listid,name,value,parent) VALUES ('1', '$NewCompany', '$NewCompany',NULL), ('2', '', '', '$NewCompany'), ('2', '$NewCustomer', '$NewCustomer', '$NewCompany'), ('3', '', '', '$NewCompany')";

if ($conn->query($sql) === TRUE) {
    header( 'Location: index.html');
    exit;
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}

    else {
    echo '<meta http-equiv="refresh" content="0;url=purchase_orders/"/>';
    exit;
}

$conn->close();


?>

So, my question here is can I simplify this code to where it will only input the data when it is not empty. So for instance instead of if (!empty($NewCompany) && empty($NewCustomer) && empty($NewTool) && empty($NewPart)) { could I just have it enter what data is inputted? In other words, instead of having 13 different if statements, only enter data based on what input boxes are not blank.

I was trying something like this:

if (!empty($NewCompany)) {
$conn->query("INSERT INTO Purchase_Orders_dynlist_items (listid,name,value,parent) VALUES ('1', '$NewCompany2', '$NewCompany2',NULL)");
}

if (!empty($NewCustomer)) {
$conn->query("INSERT INTO Purchase_Orders_dynlist_items (listid,name,value,parent) VALUES ('2', '$NewCustomer', '$NewCustomer', '$NewCompany')");
}

But this was only doing one OR the other. If both of those input fields had data, the data would not be inputted.

Maybe someone can give me a push in the right direction? Thanks!

Aucun commentaire:

Enregistrer un commentaire