Skipping empty rows on SQL update
-
Hello,
I have custom edit.php page where you can edit some details for each product in the “inventory” database. I use form with inputs and placeholders to display current product details in each form field. Here is the current code which i use to collect the data and update the product info:
include ('db.php'); if (isset($_POST['submit'])) { $prod_num = mysql_real_escape_string(htmlspecialchars($_POST['prod_num'])); $label = mysql_real_escape_string(htmlspecialchars($_POST['label'])); $quantity = mysql_real_escape_string(htmlspecialchars($_POST['quantity'])); $description = mysql_real_escape_string(htmlspecialchars($_POST['description'])); $note = mysql_real_escape_string(htmlspecialchars($_POST['note'])); $category = mysql_real_escape_string(htmlspecialchars($_POST['category'])); $public_url = mysql_real_escape_string(htmlspecialchars($_POST['public_url'])); $date = date('Y-m-d H:i:s'); global $mydb; $mydb->update("products", array( "prod_num" => $prod_num, "label" => $label, "stock" => $quantity, "description" => $description, "category" => $category, "note" => $note, "public_url" => $public_url, "modified" => $date, ), array( id => $edit_item) );
If you leave one or two fields empty the existing sql value for the same is automatically set to null. How can i skip all empty fields in the form and keep the existing (old) values in the database? I can add “if is not empty” check for each field:
if(isset($_POST['label']) &&!empty($_POST['label']){
then execute the update query, but that means 8 update queries …
Please let me know if you have any better solution. Any help is appreciated.
Thank you for your time!
- The topic ‘Skipping empty rows on SQL update’ is closed to new replies.