tgavin Posted March 29, 2008 Share Posted March 29, 2008 I have a form with a set amount of fields. If a user doesn't fill in a field it should insert nothing. Currently the word Array is being inserted How do I prevent this from inserting Array into the database if the field is left blank? escape() is just a function to clean the code (mysql_real_escape_string(), etc.) ucsmart() just capitalizes words. <?php foreach($_POST['title'] as $i => $ctitle){ $title = ucsmart(strip_tags(stripslashes($_POST['title'][$i]))); $sql = mysql_query("INSERT INTO titles (id,num,title,added_uid) VALUES (".escape($newid).",{$i},'".escape($title)."','".escape($_SESSION['uid'])."')") or die(mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98481-inserting-array/ Share on other sites More sharing options...
MadTechie Posted March 29, 2008 Share Posted March 29, 2008 sounds like a problem with your escape() function Quote Link to comment https://forums.phpfreaks.com/topic/98481-inserting-array/#findComment-504035 Share on other sites More sharing options...
tgavin Posted March 29, 2008 Author Share Posted March 29, 2008 Well, what has me scratching my head is that if the field is empty, $_POST shouldn't recognize it, right? <?php function escape($value) { // strip slashes if(get_magic_quotes_gpc()) { $value = stripslashes($value); $value = mysql_real_escape_string($value); } // quote if not integer if(!get_magic_quotes_gpc()) { if(!is_numeric($value) || $value[0] == '0') { $value = mysql_real_escape_string($value); } } return convert_smart_quotes($value); } function convert_smart_quotes($string) { $search = array( chr(145), chr(146), chr(147), chr(148), chr(151)); $replace = array( "'", "'", '"', '"', '-'); return str_replace($search, $replace, $string); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98481-inserting-array/#findComment-504099 Share on other sites More sharing options...
MadTechie Posted March 29, 2008 Share Posted March 29, 2008 Well, what has me scratching my head is that if the field is empty, $_POST shouldn't recognize it, right? Nope, if you post something thats blank it still posted from the code i you have posted i think i was wrong the bug thier but you can add a workaround function escape($value) { if(empty($value)) return ""; // NOTE: 0 with return "" but then again so will array() // strip slashes if(get_magic_quotes_gpc()) { //....snip Quote Link to comment https://forums.phpfreaks.com/topic/98481-inserting-array/#findComment-504153 Share on other sites More sharing options...
sasa Posted March 29, 2008 Share Posted March 29, 2008 can you print_r($_POST['title']); and post result Quote Link to comment https://forums.phpfreaks.com/topic/98481-inserting-array/#findComment-504165 Share on other sites More sharing options...
tgavin Posted March 29, 2008 Author Share Posted March 29, 2008 inserting "" may have fixed it. Still testing here's the print_r($_POST['title']); Array ( [1] => test one [2] => test two [3] => test 3 ) Quote Link to comment https://forums.phpfreaks.com/topic/98481-inserting-array/#findComment-504204 Share on other sites More sharing options...
discomatt Posted March 29, 2008 Share Posted March 29, 2008 My guess would be a problem with your form, you have 3 fields with the name title[] Using echo implode(', ', $_POST['title']) might get the results you want. Quote Link to comment https://forums.phpfreaks.com/topic/98481-inserting-array/#findComment-504209 Share on other sites More sharing options...
tgavin Posted March 29, 2008 Author Share Posted March 29, 2008 nope. inserting "" didn't fix it. This is print_r with empty fields. Array ( [1] => [2] => [3] => ) Quote Link to comment https://forums.phpfreaks.com/topic/98481-inserting-array/#findComment-504211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.