I am fiddling with some code to pass time, and I am unsure if I'm doing this the right way. I would really appreciate suggestions if possible of better ways of doing this.
I took a query and added a union to make essentially the same query but with a variable that has text attached to it such as 'IN'.
below you will see what i have tried, I did various things but i fear I'm wasting my time since surely there are a better way of making this work.
<?php
require_once("connection/link.php");
if(
(isset($_POST['id'])&& $_POST['id'] !='') &&
(isset($_POST['upc'])&& $_POST['upc'] !='') &&
(isset($_POST['category'])&& $_POST['category'] !='') &&
(isset($_POST['state'])&& $_POST['state'] !='') &&
(isset($_POST['quantity'])&& $_POST['quantity'] !='')
)
{
$id = $link->real_escape_string($_POST['id']);
$upc = $link->real_escape_string($_POST['upc']);
$category = $link->real_escape_string($_POST['category']);
$quantity = $link->real_escape_string($_POST['quantity']);
$state = "IN";
$query = mysqli_query($link, "SELECT * FROM products WHERE id='".$id."'");
if (!$query)
{
die('Error: ' . mysqli_error($link));
}
if(mysqli_num_rows($query) > 0){
echo "Product code already exists";
} else
{
$sql="INSERT INTO products (id, upc, category, quantity)
VALUES ('".$id."', '".$upc."', '".$category."', '".$quantity."')
UNION
INSERT INTO transactions (id, upc, category, quantity, state)
VALUES ('".$id."', '".$upc."', '".$category."', '".$quantity."', '".$state."')";
if(!$result = $link->query($sql)){
die('There was an error running the query [' . $link->error . ']');
}
else
{
echo "Product was added successfully!";
}
}
}
?>
This would create a historic log of all products added and whether they are IN or OUT. Mind you this is just the IN part of this whole ordeal.