Wagtail Posted January 18, 2014 Share Posted January 18, 2014 Hi everyone, Happy New Year! Could someone please help me with the following? I'm using a form to update a database table but I'm not sure what to do with values that are optional/not required. Do I set the value to NULL? Also, should I validate any text inputs with FILTER_SANITIZE_STRING? Thank you in advance! // Check if the form has been submitted: if ($_SERVER['REQUEST_METHOD'] == 'POST') { $errors = array(); // Trim all the incoming data: $trimmed = array_map('trim', $_POST); // Assume invalid values: $name = $order_quantity = FALSE; // Check for a name: if (empty($_POST['name'])) { $errors['name'] = 'You forgot to enter your name.'; } else { $name = mysqli_real_escape_string($dbc, $trimmed['name']); } // Check for the order quantity: if (filter_var($trimmed['order_quantity'], FILTER_VALIDATE_INT, array('min_range' => 1))) { $oq = mysqli_real_escape_string ($dbc, $trimmed['order_quantity']); } else { $oq = NULL; } if ($name && $oq ) { // If everything's OK... // Make the query: $q =”UPDATE.... Link to comment Share on other sites More sharing options...
HartleySan Posted January 18, 2014 Share Posted January 18, 2014 For optional values, if the user enters something, I would validate it. If it's valid, insert it into the DB. If it's not valid, I would spit back an error message to the user. And if they didn't input anything, I would enter NULL, an empty string, or 0, depending on what you feel is most appropriate. I personally see NULL as meaning corresponding data does not exist, which feels a bit odd for optional data. I would use NULL if, for example, you have a users table with various types of users, and some types of users have certain types of data that other users don't require. In that case, I would use NULL for the non-applicable data. Just my two cents. Link to comment Share on other sites More sharing options...
Wagtail Posted January 21, 2014 Author Share Posted January 21, 2014 Thank you for getting back to me HartleySan. Just one question. Before I make the query to update the database, there is a condition to check if the variables are true: if ($name && $oq ) But if I set $oq to NULL then that variable is false, which means I can only assign an empty string or 0 to $oq in order for the condition to be true. Is that correct? Thank you! Link to comment Share on other sites More sharing options...
Larry Posted February 4, 2014 Share Posted February 4, 2014 Yes, that's correct. You'd want to change your logic there according to what are acceptable values. Link to comment Share on other sites More sharing options...
Wagtail Posted March 1, 2014 Author Share Posted March 1, 2014 Ok, thank you Larry. I see that this thread has been viewed 1,580 times. Is this accurate? Link to comment Share on other sites More sharing options...
HartleySan Posted March 2, 2014 Share Posted March 2, 2014 I didn't even know that you could view the number of times a thread has been viewed. Link to comment Share on other sites More sharing options...
Larry Posted March 3, 2014 Share Posted March 3, 2014 Those numbers most certainly reflect search engine "viewings" as well. Link to comment Share on other sites More sharing options...
Wagtail Posted March 4, 2014 Author Share Posted March 4, 2014 Probably, I just thought they were a bit high compared to the viewings my other posts received - but it doesn't really matter. I'm enjoying your book Larry, thanks. Link to comment Share on other sites More sharing options...
Larry Posted March 5, 2014 Share Posted March 5, 2014 Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts