...

View Full Version : !is_numeric($value) isn't working properly



DiarYofaMadmaN
05-26-2004, 03:26 AM
if ($_SESSION['check'] == 1) {
$validate = array($_POST[$Category] , $_POST[$Subcategory] , $_POST[$ProductNumber] , $_POST[$ProductName] , $_POST[$Manufacturers] , $_POST[$Descriptions] , $_POST[Instock] , $_POST[$Quantity] , $_POST[$OurCost] , $_POST[$SellPrice] , $_POST[$TaxPrice] , $_POST[$Dementions] , $_POST[$Weight] , $_POST[$WeightCheck]);
$radio = array($_POST[$Instock] , $_POST[$WeightCheck]);
$numbers = array($_POST[$ProductNumber] , $_POST[$Quanity] , $_POST[$OurCost] , $_POST[$SellPrice] , $_POST[$TaxPrice] , $_POST[$Dementions] , $_POST[$Weight]);

$errors='';

foreach($_POST as $var=>$value){
echo strlen($value).'<br>';
if (strlen($value)<0){
if ((in_array($var, $numbers)) && (!is_numeric($value))){
$errors .= 'Field ' . $var . ' is required to be filled out in numberic form.<br>';
} else {
$errors .= 'Field ' . $var .' is required to be filled out.<br>';
}
}
}

}

The problem i think is with this if ((in_array($var, $numbers)) && (!is_numeric($value))){ I'm typing in a nonnumeric into the field that requires to be numeric but it just prints out Field ..... is required to be filled out. This doesn't make any sence because I tried some error trapping and debugging and printed out the string lenght and the array values and they match up perfectly so I'm not too sure why this is not working... Any help will be greatly appreciated :-)

firepages
05-26-2004, 04:54 AM
without seeing the rest of your code its a guess but

=array( $_POST[$Category] , $_POST[$Subcategory] , etc

should most likely be

=array( $_POST['Category'] , $_POST['Subcategory'] , etc

etc.

DiarYofaMadmaN
05-28-2004, 01:21 AM
without seeing the rest of your code its a guess but

=array( $_POST[$Category] , $_POST[$Subcategory] , etc

should most likely be

=array( $_POST['Category'] , $_POST['Subcategory'] , etc

etc.


<?PHP session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Add product to Database</title>
<?php



if ($_SESSION['check'] == 1) {
$validate = array($_POST['Category'] , $_POST['Subcategory'] , $_POST['ProductNumber'] , $_POST['ProductName'] , $_POST['Manufacturers'] , $_POST['Descriptions'] , $_POST['Instock'] , $_POST['Quantity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight'] , $_POST['WeightCheck']);
$radio = array($_POST['Instock'] , $_POST['WeightCheck']);
$numbers = array($_POST['ProductNumber'] , $_POST['Quanity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight']);

$errors='';

foreach($_POST as $var=>$value){
echo strlen($value).'<br>';

if (strlen($value)==0){
if ((in_array($var, $numbers)) && (!is_numeric($value))){
$errors .= 'Field ' . $var . ' is required to be filled out in numberic form.<br>';
} else {
$errors .= 'Field ' . $var .' is required to be filled out.<br>';
}
}
}

}

include 'dbconnect-include.php';

$TableName = maxforcepc;

$link = mysql_connect ($Host, $User, $Password);
mysql_select_db($DBName, $link);

$add = "INSERT INTO products (Cat_ID, Sub_Cat_ID, Product_Number, Product, Man_ID, Descriptions, In_Stock, Show_Quanity, Quanity, Cost, Sell_Price, Taxable, Tax_Price, Dementions, Weight) VALUES (" . $_POST['Category'] . ", " . $_POST['Subcategory'] . ", " . $_POST['Subcategory'] . ", " . $_POST['ProductNumber'] . ", " . $_POST['ProductName'] . ", " . $_POST['Manufacturers'] . ", " . $_POST['Descriptions'] . ", " . $_POST['InStockCheck'] . ", " . $_POST['ShowQCheck'] . ", " . $_POST['Quanity'] . ", " . $_POST['OurCost'] . ", " . $_POST['SellPrice'] . ", " . $_POST['TaxableCheck'] . ", " . $_POST['TaxPrice'] . ", " . $_POST['Dementions'] . ", " . $_POST['Weight'] . ")";
$add_to_db = mysql_query ($add);

function run_query($col, $table) {
$query = "SELECT $col FROM $table";

$Result = mysql_query ($query) or die(mysql_error());

if ($Result) {
echo "<select name='$col' id='$col'><option></option>";
while ($Row = mysql_fetch_array($Result)) { echo "<option>".$Row[$col]."</option>"; }
} else {
echo "The query could not be executed!";
}

mysql_close ($link);
print ("</select>");
}
?>
<style type="text/css">
<!--
.Titles {
margin:0 0 0 5px;
padding:0 0 0 5px;
}
.txtfieldmrgbtm {
margin-bottom:2px;
padding-bottom:2px;
border-bottom:1px solid black;
}
-->
</style></head>

<body>
<form name="form1" method="post" action="<? $_SESSION['check'] = 1; $_SERVER['PHP_SELF']; ?>">
<table width="427" border="0" align="center" cellpadding="0" cellspacing="0" style="border:2px solid black;">
<tr>
<td colspan="3" class="Titles txtfieldmrgbtm">
<b>Titles in <font color="#FF0000">RED</font> must be filled out completely!<br>
</b>
<?
echo $errors;
?>
</td>
</tr>
<tr bgcolor="#FFFFCC">
<td colspan="3" style="border-bottom:1px dashed black"><div align="center"><b>Information</b></div></td>
</tr>
<tr>
<td colspan="3" bgcolor="#F0F9FF"><table width="100%" border="0" cellspacing="0" cellpadding="0" class="txtfieldmrgdtm">
<tr>
<td class="Titles txtfieldmrgbtm" id="1"><b><font color="#FF0000">Category:</font></b></td>
<td width="298" class="txtfieldmrgbtm"><? run_query(Category, categories); ?></td>
</tr>
<tr>
<td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Subcategory:</font></b></td>
<td class="txtfieldmrgbtm"><? run_query(Subcategory, subcategories); ?></td>
</tr>
<tr>
<td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Product Number:</font></b></td>
<td class="txtfieldmrgbtm"><input name="ProductNumber" type="text" id="ProductNumber"></td>
</tr>
<tr>
<td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Product Name:</font></b></td>
<td class="txtfieldmrgbtm"><input name="ProductName" type="text" id="ProductName"></td>
</tr>
<tr>
<td class="Titles txtfieldmrgbtm"><p><b><font color="#FF0000">Manufacturers:</font></b></p>
</td>
<td class="txtfieldmrgbtm"><? run_query(Manufacturer, manufacturers); ?></td>
</tr>
<tr>
<td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Descriptions:</font></b></td>
<td class="txtfieldmrgbtm"><textarea name="Description" cols="30" rows="3" id="Description"></textarea></td>
</tr>
<tr>
<td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Instock:</font></b></td>
<td class="txtfieldmrgbtm"><input name="Instock" type="radio" value="1">
<strong>Yes</strong>
<input name="Instock" type="radio" value="0"> <strong>No</strong></td>
</tr>
<tr>
<td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Quanity:</font></b></td>
<td class="txtfieldmrgbtm"><input name="Quanity" type="text" id="Quanity"></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="3" bgcolor="#FFFFCC" style="border-bottom:1px dashed black;"><div align="center"><b>Cost Settings</b></div></td>
</tr>
<tr>
<td colspan="3" bgcolor="#F0F9FF"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="Titles txtfieldmrgbtm"><font color="#FF0000"><b>Our Cost:</b></font></td>
<td width="298" class="txtfieldmrgbtm"><input name="OurCost" type="text" id="OurCost"></td>
</tr>
<tr>
<td class="Titles txtfieldmrgbtm"><font color="#FF0000"><b>Sell Price:</b></font></td>
<td class="txtfieldmrgbtm"><input name="SellPrice" type="text" id="SellPrice"></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="3" bgcolor="#FFFFCC" style="border-bottom:1px dashed black;"><div align="center"><b>Shipping Settings</b></div></td>
</tr>
<tr>
<td colspan="3" bgcolor="#F0F9FF"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="Titles txtfieldmrgbtm"><font color="#FF0000"><b>Dementions:</b></font></td>
<td width="299" class="txtfieldmrgbtm"><input name="Dementions1" type="text" id="Dementions1" size="5">
X
<input name="Dementions2" type="text" id="Dementions2" size="5">
X
<input name="Dementions3" type="text" id="Dementions3" size="5"></td>
</tr>
<tr>
<td width="124" class="Titles txtfieldmrgbtm"><font color="#FF0000"><b>Weight:</b></font></td>
<td class="txtfieldmrgbtm"><input name="Weight" type="text" id="Weight" size="10">
<input name="weightcheck" type="radio" value="g">
<strong>g
<strong><strong><strong>
<strong><strong><strong><strong>
<input name="WeightCheck" type="radio" value="oz">
</strong></strong></strong></strong> </strong></strong> </strong>oz
<input name="WeightCheck" type="radio" value="lb">
lb
</strong></td>
</tr>
</table></td>
</tr>
<tr>
<td width="50%" bgcolor="#FFFFCC">&nbsp;</td>
<td width="50%" bgcolor="#FFFFCC">&nbsp;</td>
<td bgcolor="#FFFFCC"><div align="right">
<input type="submit" name="Submit" value="Add">
</div></td>
</tr>
</table>
</form>
</body>
</html>

There is the rest of my code, all the php codeing is in the php color code so it will be fairly easy to pick out the php coding from the regular html. The coding makes sence to me just doesn't make sence on why it's not working the way I need it to. I did some print outs of the string lenght and what not, just that It's not working correctly... Any help would be greatly appreciated :-)

raf
05-28-2004, 02:12 AM
This first chack


$numbers = array($_POST['ProductNumber'] , $_POST['Quanity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight']);
...
if ((in_array($var, $numbers)) && (!is_numeric($value))){

will never evauate to True. Your array should contain the variablenames like in your form. so
$numbers = array('ProductNumber' , 'Quanity' , 'OurCost' , 'SellPrice' , 'TaxPrice' , 'Dementions' , 'Weight');
But they need the formfields need to be exactly called like that.

DiarYofaMadmaN
05-28-2004, 02:16 AM
This first chack


$numbers = array($_POST['ProductNumber'] , $_POST['Quanity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight']);
...
if ((in_array($var, $numbers)) && (!is_numeric($value))){

will never evauate to True. Your array should contain the variablenames like in your form. so
$numbers = array('ProductNumber' , 'Quanity' , 'OurCost' , 'SellPrice' , 'TaxPrice' , 'Dementions' , 'Weight');
But they need the formfields need to be exactly called like that.

Hrmmm, I had that when I first started to code all this but it was doing the same thing, maybe I just gotten confused from changing the coding around trying different things... I'll try again and see what happends...

The problem that I'm getting is that it's not finding if it's not numeric because I can type in a non numeric character and it will not pick it up and echo me the correct error message.

raf
05-28-2004, 07:23 AM
There is also a logic error in your code, because if there is a value filled in, the
if (strlen($value)==0)
will return False, so the checks will never be made.

Maybe use the code i posted in one of your other threads :rolleyes:
http://www.codingforums.com/showthread.php?t=38920&highlight=numeric

DiarYofaMadmaN
06-02-2004, 02:09 AM
There is also a logic error in your code, because if there is a value filled in, the
if (strlen($value)==0)
will return False, so the checks will never be made.

Maybe use the code i posted in one of your other threads
http://www.codingforums.com/showthread.php?t=38920&highlight=numeric

Sorry about responding late, I went to PA with my family for a family outing kinda thing ::rolleyes:: hehe. Anyways, I really appreciate your help. PHP is all new to me right now. I am picking up sorta quickly I just need to remember lots things. However, I've been picking up what the coding does quickly by just looking at it. Just something doesn't make much sence. I need to check the form even if it is blank... So, I've changed this: if (strlen($value)>=1) to if ((strlen($value)>=1) || (strlen($value)==0))


if ($_SESSION['check'] == 1) {
$validate = array($_POST['Category'] , $_POST['Subcategory'] , $_POST['ProductNumber'] , $_POST['ProductName'] , $_POST['Manufacturers'] , $_POST['Descriptions'] , $_POST['Instock'] , $_POST['Quantity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight'] , $_POST['WeightCheck']);
$radio = array($_POST['Instock'] , $_POST['WeightCheck']);
$numbers = array($_POST['ProductNumber'] , $_POST['Quanity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight']);

$errors='';

foreach($_POST as $var=>$value){
echo strlen($value).'<br>';

if ((strlen($value)>=1) || (strlen($value)==0)){
if ((in_array($var, $numbers)) && (!is_numeric($value))){
$errors .= 'Field ' . $var . ' is required to be filled out in numberic form.<br>';
} else {
$errors .= 'Field ' . $var .' is required to be filled out.<br>';
}
}
}

}

The problem I have is I keep getting this executed: $errors .= 'Field ' . $var .' is required to be illed out.<br>'; and I should be getting $errors .= 'Field ' . $var . ' is required to be filled out in numberic form.<br>'; executed on the numeric fields... There are many sections of this project for myself that I'm doing that I've coded and finished by myself, just simple stuff like this cause me problems.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum