flemingmike Posted September 21, 2010 Share Posted September 21, 2010 hi, is there a way to do a check for blank fields before posting to database? dumb users keep adding blank fields. i need to check fields $_POST[company], $_POST[jobNO] and $_POST[staff]. if(isset($_POST['add'])) { $cname=$_POST[company]; $compid = mysql_query("SELECT * FROM company WHERE company = '$cname'"); while($row4 = mysql_fetch_array($compid)) { $cid=$row4["ID"]; } $query = "INSERT INTO jobno VALUES ( NULL, '$_POST[jobNO]', '$cid' )"; mysql_query($query) or die('Error, insert failed'); $query1 = "INSERT INTO staff VALUES ( NULL, '$_POST[staff]', '$_POST[Y]-$_POST[M]-$_POST[D]', '$_POST[jobNO]' )"; mysql_query($query1) or die('Error, insert failed1'); echo "1 record added"; Link to comment https://forums.phpfreaks.com/topic/214039-check-for-blank-fields/ Share on other sites More sharing options...
mraza Posted September 21, 2010 Share Posted September 21, 2010 what you can do is you can check like if(empty($string) or other way around is to count the string lenght with srlen and then process like if (strlen($string) < 5 ) { echo "add text"; } else { // perform ur query } Link to comment https://forums.phpfreaks.com/topic/214039-check-for-blank-fields/#findComment-1113865 Share on other sites More sharing options...
flemingmike Posted September 21, 2010 Author Share Posted September 21, 2010 can i do that for 3 different strings? Link to comment https://forums.phpfreaks.com/topic/214039-check-for-blank-fields/#findComment-1113867 Share on other sites More sharing options...
mraza Posted September 21, 2010 Share Posted September 21, 2010 sure you can check on these like if ((strlen($string) < 5) && (strlen($secondstring) < 5) && (strlen($thirdstring) < 5) ) { echo "add text"; } else { // perform ur query } Link to comment https://forums.phpfreaks.com/topic/214039-check-for-blank-fields/#findComment-1113872 Share on other sites More sharing options...
rwwd Posted September 21, 2010 Share Posted September 21, 2010 if((empty($_POST['company'])) || (empty($_POST['jobNO]')) || (empty($_POST[staff]))){ echo "Please fill in the fields, all are required"; exit; } That's the most obvious thing that comes to mind... And pop error_reporting(E_ALL); on, as the script as it stands should have thrown an error, as you aren't quoting the array index's.. @mraza; with all due respect, that's not very friendly to the user, what if they only need to type in a two/three letter value, just thought I would point that out... Rw Link to comment https://forums.phpfreaks.com/topic/214039-check-for-blank-fields/#findComment-1113873 Share on other sites More sharing options...
Pikachu2000 Posted September 21, 2010 Share Posted September 21, 2010 Here ya go. Just repeat the if conditional for each field. Lemme know if you need help. <?php // After validating that form has been submitted . . . array_map('trim', $_POST); // trim leading and trailing whitespace from all elements of $_POST array. if( empty($_POST['field']) ) { // validation fails, do something evil, dastardly, nefarious, or just outright mean } else { // validation passes, so do something completely different, or possibly nothing at all, depending on your mood. } ?> Link to comment https://forums.phpfreaks.com/topic/214039-check-for-blank-fields/#findComment-1113874 Share on other sites More sharing options...
mraza Posted September 21, 2010 Share Posted September 21, 2010 @rwwd well if i am not wrong in empty if a user give a space that would be counted as well so user can skip that checking and perform action so in my opinion is good to count there words length and to work with your code properly need to add trim check before you perform this code like trim($_POST['company']) then your code of checking Link to comment https://forums.phpfreaks.com/topic/214039-check-for-blank-fields/#findComment-1113875 Share on other sites More sharing options...
rwwd Posted September 21, 2010 Share Posted September 21, 2010 dumb users keep adding blank fields I just gave what was asked, but yes you are right, but spaces have their ascii values too, so strlen() would have the same effect, realistically, preg_match would *technically* be the best course of action in this instance but I can't be bothered to work it out.. And as correctly pointed out array_maps callback feature is really good in this scenario, but it needs to be assigned to a var, as it returns an array - hence why I overwrite the $_POST in other threads, works perfectly well Rw Link to comment https://forums.phpfreaks.com/topic/214039-check-for-blank-fields/#findComment-1113876 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.