Using If statement to exclude empty rows and columns in MySQL using PHP on insert

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?

+4
source share
3 answers

You can put the values ​​in an array and then check if they are empty or not (maybe some other checks will be recommended for security issues) and create a $ values ​​variable that you can load into the database.

 $item[0] = mysql_real_escape_string(trim($_POST['item1'])); $itemcost[0] = mysql_real_escape_string(trim($_POST['item1cost'])); $item[1] = mysql_real_escape_string(trim($_POST['item2'])); $itemcost[1] = mysql_real_escape_string(trim($_POST['item2cost'])); // and so on $values = ""; for ($i = 0; $i < 2; ++$i) { if ($item[$i] != "" && $itemcost[$i] != "") { $values .= "($user,'$item[$i]', '$itemcost[$i]'),"; } } /********************* * MySQL INSERT query *********************/ if ($values != "") { $values = substr($values, 0, -1); $query = "INSERT INTO monthly_expenses (`userid`, `item`, `amount`)" . " VALUES $values"; } 

You can also specify the name of the HTML input field as arrays, such as item [], and parse them directly into php arrays.

+3
source

Use client side validation before position. If the form is not completed in accordance with your requirements, the user is informed and forced to do so.

Refer to the Study Guide for reference.

0
source

You can store your html as array values.

  <input type="text" name="item[]" /> <input type="text" name="itemcost[]" /> 

Then you can loop them like this:

  $query = "INSERT INTO monthly_expenses (`userid`, `item`, `amount`) VALUES "; foreach($_POST['item'] as $key => $value){ { // Check if it not filled. if($value !== '') { $query .= "($user, '$value', $_POST['itemcost'][$key])"; // whatever you need to insert. } } // Run the query. 

I may have typos, but there is a concept.

0
source

All Articles