I am inserting a date from a form using PHP. I have 24 fields in which the user can insert data into the database. The problem is, how do I not allow empty fields to become strings in MySQL, do they remain empty from the user? None of the fields are required, and the fields are formatted as input text fields. What I thought was used is an if statement in the values section after the query; here is an example:
/************** * HTML Form **************/ <form action='insert.php' method='post'> <p> What items do you have for sale?</p> Item 1: <input type='text' name='item1'> Price: <input type='text' name='item1cost'> Item 2: <input type='text' name='item2'> Price: <input type='text' name='item2cost'> Item 3: <input type='text' name='item3'> Price: <input type='text' name='item3cost'> <input type="submit" name="Submit" value="Submit"> </form> /************** * PHP **************/ $user = (from $_SESSION) $item1 = $_POST['item1']; $item1cost = $_POST['item1cost']; $item2 = $_POST['item2']; $item2cost = $_POST['item2cost']; $item3 = $_POST['item3']; $item3cost = $_POST['item3cost']; /********************* * MySQL INSERT query *********************/ $query = "INSERT INTO monthly_expenses (`userid`, `item`, `amount`)" . "VALUES ($user,'$item1', '$item1cost'), ($user,'$item2', '$item2cost'), ($user,'$item3', '$item3cost'),
If the user leaves one of the fields empty, how can I make them not be inserted into the database so that there is no empty line with only the user ID?
Zowes source share