Checking to see if variable has value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Checking to see if variable has value

Post by nigma »

I have a form that you use to add rows to my database, sometimes I have to add multiple rows, so I made more input fields so I can add more than one row without having to go back to the page. But I dont always want to use these. So is there a way I can check(before adding the row) to see if the field has a value? Someone give example?

Thanks.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

My way is not foolproof but has worked quite well so far. I create an md5 hidden input for each db row based on the data I sent to the page. When they post it back I re-calc the md5. If it is different then they must have 'touched' that row. This method also allows me to avoid updating rows that have not changed.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

I don't think Kris was looking for data checksum/signature/security, rather to see if a field/value was set..

It's as simple as using empty() which returns true if the variable is not assigned, contains an empty string or the value 0..

if (!empty($_POST['somevar'])) {
# do the query
}
User avatar
DaZZleD
Forum Commoner
Posts: 38
Joined: Tue Jan 07, 2003 5:39 am

Post by DaZZleD »

also you can always use isset($variable) which returns true if the variable was set or false otherwise.
Post Reply