damo87 Posted January 24, 2010 Share Posted January 24, 2010 I have a diet related application, which has a form that asks users to enter the number of servings they eat of certain foods. The data is sent to the processing page as an array ('qty' in the code below). I need to make sure they enter a number between 1 and 999. I've been researching, and mucking around for a few days on this, and the code below is as close to working as I can get it. The first part, which checks that there are no blanks, works fine. I'm pretty sure the regex is fine as I have tested that independently. But the validation doesn't work. It seems to accept anything I enter - numbers and letters. I'm not sure if using 'implode' is the way to go, but it was the only example I could find that I could really understand. Any help greatly appreciated. Please note I am new at php, so please be gentle! # check for qty not blank? if($_POST['qty']) { foreach((array)$_POST['qty'] as $qtynull) { if (empty($qtynull)) { die('You did not enter anything. <a href="signup3.php">try again</a>.'); } } } $quantity_valid= (array)$_POST['qty']; $qv=implode(' ',$quantity_valid); if(preg_match("/[1-9]\d{0,2}$/i", $qv)){ # Any selections checked? if($_POST['qty']) { $i=0; $select = $_POST['selectionsid']; foreach((array)$_POST['qty'] as $qty) { $query = "update selections set qty='$qty' where selectionsid='$select[$i]'"; $i+=1; $result = mysql_query($query) or die("Query failed : " . mysql_error()); } } }else { echo "Incorrect format. Please enter a number between 1 and 999\n $qv"; } Quote Link to comment https://forums.phpfreaks.com/topic/189658-array-validation/ Share on other sites More sharing options...
MadTechie Posted January 24, 2010 Share Posted January 24, 2010 try this <?php //START DEBUG DATA $_POST['qty'] = array(1,2,"a",-1,3); $_POST['selectionsid'] = array("A","B","C","D","E"); //END DEBUG DATA $select = $_POST['selectionsid']; //Loop though and validate (as all will be numbers just need to check for the range) foreach($qtyArray as $K => $qty){ $qty = (int)$qty; //convert to int if($qty >= 1 && $qty <= 999){ $query = "update selections set qty='$qty' where selectionsid='$select[$K]'"; echo "$query\n"; //DEBUG output #$result = mysql_query($query) or die("Query failed : " . mysql_error()); } } update selections set qty='1' where selectionsid='A' update selections set qty='2' where selectionsid='B' update selections set qty='3' where selectionsid='E' EDIT: updated (was being dumb, no need to create function) Quote Link to comment https://forums.phpfreaks.com/topic/189658-array-validation/#findComment-1000988 Share on other sites More sharing options...
damo87 Posted January 25, 2010 Author Share Posted January 25, 2010 Many thanks, Mad Techie. Should've realised I didnt need the regex for such a simple validation. Quote Link to comment https://forums.phpfreaks.com/topic/189658-array-validation/#findComment-1001023 Share on other sites More sharing options...
MadTechie Posted January 25, 2010 Share Posted January 25, 2010 Only simple when you know the answer If this is solved please click solved bottom left, it saves the time for people who help and welcome to PHP Freaks EDIT: Oh you did solved it Quote Link to comment https://forums.phpfreaks.com/topic/189658-array-validation/#findComment-1001025 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.