Log in

View Full Version : Checking Fields



Titan85
04-23-2007, 12:39 AM
Hello, I actually have 2 questions. The first is I am trying to make a scirpt that will make sure that a file field is filled in. For normal text or password fields I use
if (!$whatever) {I tried that and I also tried
if (strlen($whatever) < 1) {Neither one worked out. I am guessing it is because you can not check an image fields contents because it is made for uploading. Is there a way I can check to make sure it is filled in?

Also, is there a way to have a search look for anything that looks has a few letters matching? I am thinking of using eregi(), but I am trying to search a mysql database and do not know what I need to search in (the sting that I search for the pattern within it). How can I do this?

Thanks

mburt
04-23-2007, 12:49 AM
Use the isset() function:

if (!isset($whatever)) { // it is NOT set

Titan85
04-23-2007, 03:31 AM
I tried that and even if there is a file specified it gives the error I specified if it is not set. Any ideas? Also, does anyone have an idea on what I should do with the search?

boxxertrumps
04-23-2007, 03:34 AM
if ($var !== ("" || null)) {

Try that...

mwinter
04-23-2007, 10:22 AM
The first is I am trying to make a scirpt that will make sure that a file field is filled in.

Use the isset (http://uk.php.net/manual/en/function.isset.php) function to examine the $_FILES super-global array:



if (isset($_FILES['control-name'])) {
/* File uploaded */
}

You'll probably want to perform further analysis to ensure the user sent what you wanted. See the PHP documentation for more information.



For normal text or password fields I use
if (!$whatever) {

I would recommend more strenuous tests based on regular expressions. For example, sending a string of white space would still pass that test, but be completely useless. Probably.



I tried that and I also tried
if (strlen($whatever) < 1) {Neither one worked out. I am guessing it is because you can not check an image fields contents because it is made for uploading. Is there a way I can check to make sure it is filled in?

Are you actually trying to examine the input field? You cannot: it is a means to an end and not actually sent to the server. Only the uploaded data, if any, is transmitted.



Also, is there a way to have a search look for anything that looks has a few letters matching? I am thinking of using eregi(), but I am trying to search a mysql database and do not know what I need to search in (the sting that I search for the pattern within it). How can I do this?

There are pattern matching search techniques in SQL. For example,

&#160;&#160;SELECT * FROM table WHERE forename LIKE '%ic%'

would match names like Michael, Jessica, etc. Some database engines, including MySQL, support regular expressions, too. See the reference for string comparison functions in MySQL (http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html) for a start.



if ($var !== ("" || null)) {

That would be equivalent to:



if ($var !== false) {

as the OR logical operator (||) always yields a boolean.

Mike

Titan85
04-23-2007, 07:51 PM
Ok, i got the image checker working, but the search still isn't. Now it is not returning results at all. Here is my code:
$get = mysql_query("SELECT * FROM `teams` WHERE name LIKE '%$value%'") or die ("Error Searching Teams! \n<br />\n" .mysql_error()); Value is:
$value = mysql_real_escape_string($_POST['value']);Is it not reading what value is? Thanks

mwinter
04-24-2007, 05:56 PM
Here is my code:
$get = mysql_query("SELECT * FROM `teams` WHERE name LIKE '%$value%'") or die ("Error Searching Teams! \n<br />\n" .mysql_error()); Value is:
$value = mysql_real_escape_string($_POST['value']);Is it not reading what value is?

I don't see any obvious problems. Try echoing or logging the values to make sure that they are what you expect. For instance, include the following somewhere before the query:



echo "<!-- {$value}\n";
var_dump($_POST);
echo " -->\n";

The comment, beginning at the second line, may be quite long depending upon how much data was submitted via the POST method.

Mike

Titan85
04-24-2007, 07:52 PM
Ok, its working now. I thought that I had some data in the database like what I was searching, but I actually didn't :/. Just my stupid mistake. Thanks for the help.