Forum Moderators: coopster

Message Too Old, No Replies

PHP Beginner - compare a form value against values in a table

compare a form value against values in a table

         

sharks3010

12:07 pm on Mar 25, 2009 (gmt 0)

10+ Year Member



Hi,

This will probably appear quite basic, however I am in need of help.

I have a SQL database with an appropriate table. (as shown below)

-- Table structure for table `costlimits`
--

CREATE TABLE IF NOT EXISTS `costlimits` (
`limID` tinyint(3) unsigned NOT NULL,
`limMin` smallint(5) unsigned DEFAULT NULL,
`limMax` smallint(5) unsigned DEFAULT NULL,
`limCost` decimal(3,2) unsigned DEFAULT NULL,
PRIMARY KEY (`limID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

I want to be able to take a number submitted in a form field, and compare the value of that field to the limMin and limMax entries in order to retrieve the appropriate value from limCost. So simply, if:

x is between limMin and limMax return limCost

Hope that explains it,

any help would be greatly appreciated.

Thanks

rocknbil

3:41 pm on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard sharks3010!

(for example)
<input type="text" name="some-value" value="17">

limID limMin limMax limCost
1.....0......5......1
2.....5......10.....3
3.....10.....15.....7
4.....15.....20.....9
5.....20.....25.....12

$input = dont_forget_to_cleanse($_POST['some-value']);

$select = "select limCost from costlimits where limMin <= '$input' and limMax > '$input'";

The above should return 9. Note you only want one of these <= or >= (less than/greater than OR equal to.) Otherwise it would hook both rows 3 and 4 if the input is, say, 15.

sharks3010

8:24 am on Mar 26, 2009 (gmt 0)

10+ Year Member



thanks a lot for the welcome and your help. I'll try that, I think that will be all I need, however if I do have any issues I'll post them back.

Thanks again

David