liquinas Posted February 25, 2007 Share Posted February 25, 2007 Hello. I have here a standard form with some additional code to check that fields are not left blank. However, fields $L1 through $L20 I want those fields to be allowed to be left blank. Anything I could do to exclude these fields from being required? <?php $lol = $_GET['lol']; ?> <?php $user="liquinas"; $host="localhost"; $password="roflmao"; $database = "tlcmotors"; $connection = mysql_connect($host,$user,$password) or die ("An error occured while accessing the database. Please contact [email protected]"); $carinfo = getcarinfo("$lol"); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Super Duper Database Management System 2k7</title> </head> <body> <?php $user="liquinas"; $host="localhost"; $password="roflmao"; $database = "tlcmotors"; $connection = mysql_connect($host,$user,$password) or die ("An error occured while accessing the database. Please contact [email protected]"); $carinfo = getcarinfo("$lol"); $DB = mysql_select_db($database,$connection) or die ("Error selecting database. Please contact [email protected]"); /* check information from the form */ /* set up array of field labels */ $label_array = array ( "List_title" => "List_title", "Short_title" => "Short_title", "VIN" => "VIN", "ID" => "ID", "Engine" => "Display_engine", "Transmission" => "Transmission", "Color" => "Color", "Miles" => "Miles", "Display_miles" => "Display_miles", "Style" => "Style", "Make" => "Make", "Price" => "Price", "Display_price" => "Display_price", "Year" => "Year"); foreach ($HTTP_POST_VARS as $key => $value) { /* check each field for blank fields */ if ($value == "") { $blank_array[$key] = "blank"; } elseif ( ereg("(name)",$key) ) //if key includes "name" { if (!ereg("^[A-Za-z' -]{1,50}$",$HTTP_POST_VARS[$key]) ) { $bad_format[$key] = "bad"; } } } /* If any fields were not okay, display error message and form */ if (@sizeof($blank_array) > 0 or @sizeof($badformat) > 0) { if (@sizeof($blank_array) > 0) { /* Display message for missing information */ echo "<b>You didn't fill in one or more required fields. You must enter:</b><br>"; /* Display listing of missing information */ foreach($blank_array as $key => $value) { echo " {$label_array[$key]}<br>"; } } if (@sizeof($bad_format) > 0) { /* Display message for bad information */ echo "<b>One or more fields have information that appear to be incorrect. This means it has an incorrect format (i.e. inserting comas or symbols on a text-only field). Please correct the format for the following fields:</b><br>"; /* Display list of incorrect information */ foreach($bad_format as $key => $value) { echo " {label_array[$key]}<br>"; } } /* REDISPLAY THE FORM */ echo "FORM GOES HERE"; exit(); } else //if data is okay { $query = "INSERT INTO Cars (ID,List_title,Short_title,VIN,Year,Make,Engine,Display_engine,Transmission,Color,Miles,Display_miles,Style,Price,Display_price,L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12,L13,L14,L15,L16,L17,L18,L19,L20,Pic_1,Pic_2,Pic_3,Pic_4,Thumb_1,Thumb_2,Thumb_3,Thumb_4) VALUES('$ID','$List_title','$Short_title','$VIN','$Year','$Make','$Engine','$Display_engine','$Transmission','$Color','$Miles','$Display_miles','$Style','$Price','$Display_price','$L1','$L2','$L3','$L4','$L5','$L6','$L7','$L8','$L9','$L10','$L11','$L12','$L13','$L14','$L15','$L16','$L17','$L18','$L19','$L20','$Pic_1','$Pic_2','$Pic_3','$Pic_4','$Thumb_1','$Thumb_2','$Thumb_3','$Thumb_4')"; $result = mysql_query($query) or die ("Problem with Query. Contact [email protected]"); echo "Database has been updated<br>"; } ?> </body> </html> <?php function getcarinfo($ID) { $db = mysql_select_db("tlcmotors") or die ("Error-Could not select database. Please contact [email protected]"); $query = "SELECT * FROM cars WHERE ID='$ID'"; $result = mysql_query($query) or die ("Error-Could not execute query. Please contact [email protected]"); return mysql_fetch_array($result,MYSQL_ASSOC); } ?> Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/ Share on other sites More sharing options...
liquinas Posted February 25, 2007 Author Share Posted February 25, 2007 bump Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-193350 Share on other sites More sharing options...
superuser2 Posted February 25, 2007 Share Posted February 25, 2007 I haven't tested this but try: <?php $allowed_fields = array(); $i = 1; while($i < 20) { $i++; $allowed_fields[] = 'L' . $i; } if($value == "" && !in_array($key, $allowed_fileds)) { The if() should not run if the value is L1 through L20. Once again, I didn't test this, but try it and let us know what happens. Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-193356 Share on other sites More sharing options...
btherl Posted February 25, 2007 Share Posted February 25, 2007 Looks fine to me except for the incrementing.. that'll give you L2 -> L20. Just start with $i = 0 and that will fix it. You can also use either foreach (range(1,20) as $i) { $allowed_fields[] = 'L' . $i; } or a C style for loop: for ($i = 1; $i <= 20; $i++) { $allowed_fields[] = 'L' . $i; } Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-193433 Share on other sites More sharing options...
superuser2 Posted February 25, 2007 Share Posted February 25, 2007 Yeah, correction on mine, full code: <?php $allowed_fields = array(); $i = 0; while($i < 20) { $i++; $allowed_fields[] = 'L' . $i; } if($value == "" && !in_array($key, $allowed_fileds)) { Thanks for catching that, btherl Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-193607 Share on other sites More sharing options...
liquinas Posted February 26, 2007 Author Share Posted February 26, 2007 Sorry for my noobness, how do I implement that with my code? Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-194583 Share on other sites More sharing options...
superuser2 Posted February 26, 2007 Share Posted February 26, 2007 <?php $lol = $_GET['lol']; ?> <?php $user="liquinas"; $host="localhost"; $password="roflmao"; $database = "tlcmotors"; $connection = mysql_connect($host,$user,$password) or die ("An error occured while accessing the database. Please contact [email protected]"); $carinfo = getcarinfo("$lol"); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Super Duper Database Management System 2k7</title> </head> <body> <?php $user="liquinas"; $host="localhost"; $password="roflmao"; $database = "tlcmotors"; $connection = mysql_connect($host,$user,$password) or die ("An error occured while accessing the database. Please contact [email protected]"); $carinfo = getcarinfo("$lol"); $DB = mysql_select_db($database,$connection) or die ("Error selecting database. Please contact [email protected]"); /* check information from the form */ /* set up array of field labels */ $label_array = array ( "List_title" => "List_title", "Short_title" => "Short_title", "VIN" => "VIN", "ID" => "ID", "Engine" => "Display_engine", "Transmission" => "Transmission", "Color" => "Color", "Miles" => "Miles", "Display_miles" => "Display_miles", "Style" => "Style", "Make" => "Make", "Price" => "Price", "Display_price" => "Display_price", "Year" => "Year"); $allowed_fields = array(); $i = 0; while($i < 20) { $i++; $allowed_fields[] = 'L' . $i; } foreach ($HTTP_POST_VARS as $key => $value) { /* check each field for blank fields */ if ($value == "" && !in_array($key, $allowed_fileds)) { $blank_array[$key] = "blank"; } elseif ( ereg("(name)",$key) ) //if key includes "name" { if (!ereg("^[A-Za-z' -]{1,50}$",$HTTP_POST_VARS[$key]) ) { $bad_format[$key] = "bad"; } } } /* If any fields were not okay, display error message and form */ if (@sizeof($blank_array) > 0 or @sizeof($badformat) > 0) { if (@sizeof($blank_array) > 0) { /* Display message for missing information */ echo "<b>You didn't fill in one or more required fields. You must enter:</b><br>"; /* Display listing of missing information */ foreach($blank_array as $key => $value) { echo " {$label_array[$key]}<br>"; } } if (@sizeof($bad_format) > 0) { /* Display message for bad information */ echo "<b>One or more fields have information that appear to be incorrect. This means it has an incorrect format (i.e. inserting comas or symbols on a text-only field). Please correct the format for the following fields:</b><br>"; /* Display list of incorrect information */ foreach($bad_format as $key => $value) { echo " {label_array[$key]}<br>"; } } /* REDISPLAY THE FORM */ echo "FORM GOES HERE"; exit(); } else //if data is okay { $query = "INSERT INTO Cars (ID,List_title,Short_title,VIN,Year,Make,Engine,Display_engine,Transmission,Color,Miles,Display_miles,Style,Price,Display_price,L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12,L13,L14,L15,L16,L17,L18,L19,L20,Pic_1,Pic_2,Pic_3,Pic_4,Thumb_1,Thumb_2,Thumb_3,Thumb_4) VALUES('$ID','$List_title','$Short_title','$VIN','$Year','$Make','$Engine','$Display_engine','$Transmission','$Color','$Miles','$Display_miles','$Style','$Price','$Display_price','$L1','$L2','$L3','$L4','$L5','$L6','$L7','$L8','$L9','$L10','$L11','$L12','$L13','$L14','$L15','$L16','$L17','$L18','$L19','$L20','$Pic_1','$Pic_2','$Pic_3','$Pic_4','$Thumb_1','$Thumb_2','$Thumb_3','$Thumb_4')"; $result = mysql_query($query) or die ("Problem with Query. Contact [email protected]"); echo "Database has been updated<br>"; } ?> </body> </html> <?php function getcarinfo($ID) { $db = mysql_select_db("tlcmotors") or die ("Error-Could not select database. Please contact [email protected]"); $query = "SELECT * FROM cars WHERE ID='$ID'"; $result = mysql_query($query) or die ("Error-Could not execute query. Please contact [email protected]"); return mysql_fetch_array($result,MYSQL_ASSOC); } ?> Try that. If it doesn't work, please let me know. Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-194654 Share on other sites More sharing options...
liquinas Posted February 26, 2007 Author Share Posted February 26, 2007 That results in Warning: in_array(): Wrong datatype for second argument in c:\apache\htdocs\projects\tlc_motors\process.php on line 83 line 83 is if ($value == "" && !in_array($key, $allowed_fileds)) Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-194775 Share on other sites More sharing options...
liquinas Posted February 26, 2007 Author Share Posted February 26, 2007 I am willing to send up to 3 cookies to whoever helps me fix this today Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-194813 Share on other sites More sharing options...
liquinas Posted February 26, 2007 Author Share Posted February 26, 2007 I have also tried setting the default value for the text fields to but this only works when using the form to insert a new record. When using the update form, it shows up as "" again because the default value for the text field comes from the database and it prints it as empty rather than the actual code. Any other solution to this prob would be greatly appreciated, I'm totally stuck and in deep sh** if I can't get this working. Thanks. Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-194839 Share on other sites More sharing options...
superuser2 Posted February 27, 2007 Share Posted February 27, 2007 Right before the foreach do die(print_r($allowed_fields)); and tell me what that outputs. Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-194858 Share on other sites More sharing options...
liquinas Posted February 27, 2007 Author Share Posted February 27, 2007 Array ( [0] => L1 [1] => L2 [2] => L3 [3] => L4 [4] => L5 [5] => L6 [6] => L7 [7] => L8 [8] => L9 [9] => L10 [10] => L11 [11] => L12 [12] => L13 [13] => L14 [14] => L15 [15] => L16 [16] => L17 [17] => L18 [18] => L19 [19] => L20 ) 1 the 1 at the end is correct Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-194861 Share on other sites More sharing options...
liquinas Posted February 27, 2007 Author Share Posted February 27, 2007 Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-194906 Share on other sites More sharing options...
superuser2 Posted February 27, 2007 Share Posted February 27, 2007 Sorry spelling error: if ($value == "" && !in_array($key, $allowed_fileds)) should be if ($value == "" && !in_array($key, $allowed_fields)) Try it again, that should work. Sorry, I just spelled fields wrong. Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-194914 Share on other sites More sharing options...
superuser2 Posted February 27, 2007 Share Posted February 27, 2007 and by the way when you do that take out the die(print_r thing. Link to comment https://forums.phpfreaks.com/topic/39980-ignoring-a-field-with-script-that-checks-for-blank-field/#findComment-194916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.