Forum Moderators: coopster

Message Too Old, No Replies

Conditions not met .. cannot stop exec rest of scritp

Why is script executed w/wrong conditions

         

henry0

12:45 pm on Apr 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am missing something obvious
Here is a simplified version of my problem
If conditions are not corresponding to the script requirements

The rest of the script (the query) is still executed
Although "echo" shows the corresponding warning
Nevertheless the query is executed
How can I be sure that if conditions are not met
then the query is not exec
Thank you
<?php

if (strlen($_POST['username'])<1 ¦¦

{
echo '<div align="center">Sorry, Go Back - Missing Fields -.</div>';
}
if (strlen($_POST['username'])<4 ¦¦
strlen($_POST['username'])>12 ¦¦

{
echo '<div align="center">Username and Password must be between 4 and 16 Characters.</div>';
}

?>
<?
if (strlen($_POST['username'])>3 ¦¦
etc....
{
$username=$_POST['username'];
$password=$_POST['password'];
$page=$_POST['page'];
$business_name=$_POST['business_name'];
$full_name=$_POST['full_name'];
$description=$_POST['description'];

$query = "INSERT INTO eco_dev (username, password, etc.................. #########
$result= mysql_query ($query);
}
else
{
echo "Sorry, you need to fix a few problems, please click your back browser";
}
?>

henry0

11:49 am on Apr 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Any one has an idea on Easter day?
thanks

ergophobe

4:05 pm on Apr 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your "if" statements are not using any of the allowed syntaxes. See

[php.net...]

Tom

henry0

4:45 pm on Apr 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you Tom

will double check upon the link provided
and report

Henry

henry0

5:53 pm on Apr 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do not figure it out

Those examples do not speak to me

it is still based on my typing
<<
if (strlen($_POST['username'])<1 ¦¦

{
echo '<div align="center">Sorry, Go Back - Missing Fields -.</div>';
}
>>
I need to understand where is the error?

and why is the rest of the script still executing

thanks

WhosAWhata

5:58 pm on Apr 11, 2004 (gmt 0)

10+ Year Member



if (strlen($_POST['username'])<1) {
echo '<div align="center">Sorry, Go Back - Missing Fields -.</div>';
exit;
}

henry0

6:59 pm on Apr 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks
I have already the "exit"
And the reason for the "¦¦" (although the forum script changed my double pipe)
Is that I have more that one "strlen checking line"
I shortened the script for the forum purpose
I should have motioned it, sorry,
However it does not stop the query to be executed even if the condition is not met.
Henry

ergophobe

7:09 pm on Apr 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



[Henry0, I was writing this as you were posting, so it does not take account of your last post. I know - you understand all this, but that was not clear from your previous posts. It would be alot easier to answer the question of the syntax was tested a little bit before posting!]

Let's break this down

if (strlen($_POST['username'])<1 ¦¦

Start with the if
syntax: if (expression)

You must have opening and closing parentheses around your expression. You open, but do not close. That gives:

if (strlen($_POST['username'])<1) ¦¦

Now the next problem is the

¦¦

This is the OR operator. You need another expression to follow it as in

if (expression ¦¦ expression)

Since you don't have two expressions, you cannot have an OR operator.

Two more things quickly.

First, in an if statement, an expression that evaluates to a value greater than zero or an empty string, is true. Zero or an empty string evaluates to false. So for the purposes of an expression evaluated in an if or a while, for example,

1 = "asdf" = true
0 = "" = false

If $_POST['username'] is set to "asdf" then strlen($_POST['username']) is equal to 4 which evaluates to true. So these are all equivalent (testing now for the positive case, not the negative as in your original code):


if (strlen($_POST['username']) > 0)
if (strlen($_POST['username']))
if ($_POST['username'])

Finally, you probably should test that $_POST['username'] is set. If it isn't you may get a warning depending on how your server has error reporting set (you probably won't, but it's not a bad practice). Thus your final statement becomes

if (isset($_POST['username']) && $_POST['username'])

Or, if you prefer

if (isset($_POST['username']) &&!empty($_POST['username']) )

ergophobe

7:18 pm on Apr 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Henry0,

Start by taking the if statement that encloses the query and reduce it to just the first expression. Test. Add the next expression. Test. When you see that the behavior is not as expected, you will know why.

henry0

7:29 pm on Apr 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ergophobe
the last part of your post makes lot of sense
I will test it asper your advise

thank you
Henry

henry0

8:10 pm on Apr 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ergophobe you cleaned my thread of thoughts
Sometimes I can write many pages with out a glitch
And sometimes I feel totally inadequate
I am not afraid to say that I still need to learn a lot
I went too fast too far without solid enough foundations
Another 6 months and I’ll be there

To make it short I got it working;one glitch in 500+ lines but I know that was a brain dead one

ergophobe

8:16 pm on Apr 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Ergophobe you cleaned my thread of thoughts

Many people have said that of me, but few have meant it as a compliment ;-)

Tom