Jump to content

Recommended Posts

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! :P

 

 

# 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";

}

Link to comment
https://forums.phpfreaks.com/topic/189658-array-validation/
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/189658-array-validation/#findComment-1000988
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.