Kristen L Kristen L - 24 days ago 10
MySQL Question

PHP validation if not exists price

I have two inputs Price1 and Price2. And if I have only one price what I add to Price1 and Price2 will be empty. So I want it add to Database and in database in price 2 doesnt have

PHP:

if(isset($_POST["submitmenu"])){
$menu_item_category = $_POST["menu_item_category"];
$menu_item_name = $_POST["menu_item_name"];
$menu_item_details = $_POST["menu_item_details"];
$menu_price1 = $_POST["menu_price1"];
$menu_price2 = $_POST["menu_price2"];

$query_orders = "INSERT INTO menus (menu_item_category,menu_item_name,menu_item_details, menu_price1, menu_price2)
VALUES ('$menu_item_category','$menu_item_name','$menu_item_details', '$menu_price1', '$menu_price2');";
mysqli_query($con, $query_orders);
}


SQL:

CREATE TABLE IF NOT EXISTS `menus` (
`menu_item_id` int(10) NOT NULL AUTO_INCREMENT,
`menu_item_category` varchar(100) NOT NULL,
`menu_item_name` varchar(100) NOT NULL,
`menu_item_details` varchar(300) DEFAULT NULL,
`menu_price1` double NOT NULL,
`menu_price2` double NOT NULL,
`menu_date` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`menu_item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=244 ;


Its working only when I add 2 prices.


EXAMPLE:

Price1 = 20

Price2 = 30


Then its working and sending database.
But if i dont insert price into Price2 then it not sending database.


EXAMPLE:

Price1 = 20

Price2

Answer

The issue is that your table is set up with the menu_price2 is NOT NULL. Either alter the table to allow NULL or try something like

$menu_price2 = empty($_POST["menu_price2"]) ? $_POST["menu_price2"] : 0;
Comments