We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Multipul IF ELSE's IN PHP
Topic Started: Jun 10 2005, 12:07 AM (436 Views)
Das
Member Avatar
Smells of rich mahogany
[ *  *  *  *  *  *  * ]
I know I have another thread, but the other question is gonna take a while to answer I gather, and adding another question there is only gonna confuse things so:
Quote:
 
<?
$user = $_POST["username"];
$pass = $_POST["password"];
$email = $_POST["email"];
echo "Loading....";
$dbh=mysql_connect ("localhost", "dasein_dasein", "******") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("dasein_users");
$query = "select * from members where username = '$user'";
$result = mysql_query($query);
if (mysql_num_rows($result)) {
echo "<p>Error, member name already in use.</p>";
}
if ($pass == "") {
echo "<p>Error, you must have a password silly.</p>";
}
if ($email == "") {
echo "<p>Error, you need a e-mail for us to let you know whenimportant things happen.</p>";
}
mysql_query("INSERT INTO `members` (username, password, type, email) VALUES ('$user', '$pass', 'member', '$email')") or die(mysql_error());
echo "<p>You have been succesfully registered!</p> <a href='/index.php'>Go Back To Home.</a>";
?>


As you can see there are multipul IF statements. Is there a way to use }else{ for all of them at once and then do the thing on the bottom? If it gets caught on an IF statment it means something is wrong, but if it dosn't pass all of them it means the submition was correct and then I want the query to be added.


Question2: Is there a way to use a wildcard in php? I want to make sure the user's e-mail is in proper form EX: something@something.something is this possible to do?
Offline Profile Quote Post Goto Top
 
FearKiller
Member Avatar
www.drewscripts.com
[ *  *  *  *  * ]
QUESTION 1:
Code:
 
<?
$user = $_POST["username"];
$pass = $_POST["password"];
$email = $_POST["email"];
echo "Loading....";
$dbh=mysql_connect ("localhost", "dasein_dasein", "******") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("dasein_users");
$query = "select * from members where username = '$user'";
$result = mysql_query($query);
if (mysql_num_rows($result)) {
echo "<p>Error, member name already in use.</p>";
}
elseif ($pass == "") {
echo "<p>Error, you must have a password silly.</p>";
}
elseif ($email == "") {
echo "<p>Error, you need a e-mail for us to let you know whenimportant things happen.</p>";
}
else {
mysql_query("INSERT INTO `members` (username, password, type, email) VALUES ('$user', '$pass', 'member', '$email')") or die(mysql_error());
echo "<p>You have been succesfully registered!</p> <a href='/index.php'>Go Back To Home.</a>";
}
?>


QUESTION 2:
Code:
 
if(!ereg("^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$", $email)) {
echo 'Error: Your email sucks. You either screwed up somewhere or your tryin to screw us by putin in a bunch of random characters.';
}
else {
echo 'Hell yeah the email address you have entered is kick ass. You're ready to go.';
}

There may be a better way for question number 2, but I believe that does work.

You could also use the empty() function or isset() function to check for blank fields.
Code:
 
if(empty($pass)) {
echo 'WTF! You didn't enter a password.';
}
Offline Profile Quote Post Goto Top
 
Seth
Member Avatar
I has a pony
[ *  *  *  *  *  *  *  *  * ]
And if you hope for your code to be portable, you gotta stop the short-tag stuff. The correct PHP style is
Code:
 
<?php
/* whee PHP here */
?>
Offline Profile Quote Post Goto Top
 
Das
Member Avatar
Smells of rich mahogany
[ *  *  *  *  *  *  * ]
Seth
June 9, 2005 09:42 PM
And if you hope for your code to be portable, you gotta stop the short-tag stuff. The correct PHP style is
Code:
 
<?php
/* whee PHP here */
?>

What is 'portable'?
Offline Profile Quote Post Goto Top
 
kingy
1 in 10 people understands binary, the other 1 doesn't
[ *  *  *  *  *  * ]
I think the <? short tags are being removed in the next few versions of php, phased out.

If you are going to be running a current version you will be alright unless you upgrade. I think thats what seth means
Offline Profile Quote Post Goto Top
 
Total_Resistance
Member Avatar
Forum Support // Webmaster
[ *  *  * ]
some browsers dont read <? >
Seths way will work for all upcoming browser updates
Offline Profile Quote Post Goto Top
 
Stefan
Member Avatar
Mew?
[ *  *  *  *  *  *  * ]
That has nothing to do with browsers. PHP is not visible for browsers, only the output generated by PHP. It's how PHP is configured at your server. http://docs.php.net/en/ini.html#ini.short-open-tag
Offline Profile Quote Post Goto Top
 
Gornakle
Member
[ *  *  *  *  * ]
With 'portable', he means that it will run on different servers. Some servers have the short tags disabled, or don't support them yet (not sure when short tags were introduced).
Offline Profile Quote Post Goto Top
 
Dennis
Member
[ *  *  *  *  *  *  * ]
Instead of using ($pass == ""), try this:

Quote:
 
(empty($pass))


Or

Quote:
 
(!isset($pass))
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply