Current location: Hot Scripts Forums » Programming Languages » PHP » Started php and working on an address book...need help....


Started php and working on an address book...need help....

Reply
  #1 (permalink)  
Old 10-05-07, 07:04 PM
sushi4664's Avatar
sushi4664 sushi4664 is offline
Aspiring Coder
 
Join Date: Apr 2007
Location: USA
Posts: 411
Thanks: 0
Thanked 0 Times in 0 Posts
Question Started php and working on an address book...need help....

I started learning php and am building an address and I need help. Before asking questions Ill post the code:
PHP Code:

<?

session_start
();
if (
$_SESSION[‘loggedin’] != 1
        
header("Location: index.php"); 
if(
$_SESSION['full'] != 1
        
header("Location: home.php"); 
if (!
file_exists('config.php'))
            die(
"<p>The file 'config.php' does not exist. Goto the <a href=\"setup/index.php\"> installer </a> and install this address book</p></body></html>");
include_once(
"config.php");
//********* 
if(isset($_POST['firstname'])){$firstname $_REQUEST['firstname'];} 
if(isset(
$_POST['lastname'])){$lastname $_REQUEST['lastname'];} 
if(isset(
$_POST['address'])){$address $_REQUEST['address'];} 
if(isset(
$_POST['postalcode'])){$postalcode $_REQUEST['postalcode'];} 
if(isset(
$_POST['city'])){$city $_REQUEST['city'];} 
if(isset(
$_POST['province'])){$province $_REQUEST['province'];} 
if(isset(
$_POST['country'])){$country $_REQUEST['country'];} 
if(isset(
$_POST['home'])){$home $_REQUEST['home'];} 
if(isset(
$_POST['mobile'])){$mobile $_REQUEST['mobile'];} 
if(isset(
$_POST['work'])){$work $_REQUEST['work'];} 
if(isset(
$_POST['fax'])){$fax $_REQUEST['fax'];} 
if(isset(
$_POST['email'])){$email $_REQUEST['email'];} 
if(isset(
$_POST['email2'])){$email2 $_REQUEST['email2'];} 
if(isset(
$_POST['bdayd'])){$bdayd $_REQUEST['bdayd'];} 
if(isset(
$_POST['bdaym'])){$bdaym $_REQUEST['bdaym'];} 
if(isset(
$_POST['bdayy'])){$bdayy $_REQUEST['bdayy'];} 
if(isset(
$_POST['yahoo'])){$yahoo $_REQUEST['yahoo'];} 
if(isset(
$_POST['msn'])){$msn $_REQUEST['msn'];} 
if(isset(
$_POST['icq'])){$icq $_REQUEST['icq'];} 
if(isset(
$_POST['website'])){$website $_REQUEST['website'];} 
if(isset(
$_POST['comments'])){$comments $_REQUEST['comments'];} 
if(isset(
$_POST['jabber'])){$jabber $_REQUEST['jabber'];} 
if(isset(
$_POST['gtalk'])){$gtalk $_REQUEST['gtalk'];} 
if(isset(
$_POST['aim'])){$aim $_REQUEST['aim'];} 
if(isset(
$_POST['skype'])){$skype $_REQUEST['skype'];} 
if(isset(
$_POST['relation'])){$relation $_REQUEST['relation'];} 
//***** 
if($firstname == null)
{
    print(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
    <html xmlns=\"http://www.w3.org/1999/xhtml\"> 
    <head> 
    <title>Address Book - Add</title> 
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> 
    <script language=\"javascript\" type=\"text/javascript\" src=\"niceforms.js\"></script> 
    <style type=\"text/css\" media=\"screen\">@import url(style.css);</style> 
    <style type=\"text/css\" media=\"screen\">@import url(niceforms_style.css);</style> 
    </head> 
    <body> 
    <div id=\"wrap\"> 
            <div id=\"header\"> 
                    <h1 id=\"logo-text\">Address Book</span></h1> 
                    <h2 id=\"slogan\">By Ayush Sood</h2> 
            </div> 
            <div  id=\"menu\"> 
                    <ul> 
                            <li><a href=\"home.php\">Home</a></li> 
                            <li id=\"current\"><a href=\"add.php\">Add</a></li> 
                            <li><a href=\"index.html\">Search</a></li> 
                    </ul> 
            </div> 
            <div id=\"content-wrap\"> 
                    <div id=\"main\"> 
                    <form name=\"adduser\" method=\"post\" class=\"niceform\" action=\"\"> 
                            <table width=\"100%\" border=\"0\"> 
          <tr> 
            <td width=\"50%\">First Name:<br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"firstname\" id = \"textinput\" value=\"
$firstname\" tabindex=\"1\" size=\"18\"/></td> 
            <td width=\"50%\">Email: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"email\" id = \"textinput\" value=\"
$email\" tabindex=\"13\" size=\"18\" /></td> 
          </tr> 
          <tr> 
            <td>Last Name: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"lastname\" id = \"textinput\" value=\"
$lastname\" tabindex=\"2\" size=\"18\" /></td> 
            <td>Second Email: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"email2\" id = \"textinput\" value=\"
$email2\" tabindex=\"14\" size=\"18\" /></td> 
          </tr> 
          <tr> 
            <td>Address: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"address\" id = \"textinput\" value=\"
$address\" tabindex=\"3\" size=\"18\" /></td> 
            <td>Home Phone Number: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"home\" id = \"textinput\" value=\"
$home\" tabindex=\"15\" size=\"18\"  /></td> 
          </tr> 
          <tr> 
            <td>City: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"city\" id = \"textinput\" value=\"
$city\" tabindex=\"4\" size=\"18\" /></td> 
            <td>Cell: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"mobile\" id = \"textinput\" value=\"
$mobile\" tabindex=\"16\" size=\"18\" /></td> 
          </tr> 
          <tr> 
            <td>State/Providence: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"province\" id = \"textinput\" value=\"
$province\" tabindex=\"5\" size=\"18\" /></td> 
            <td>Work Phone Number: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"work\" id = \"textinput\" value=\"
$work\" tabindex=\"17\" size=\"18\" /></td> 
          </tr> 
          <tr> 
              <td>Zip Code: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"postalcode\" id = \"textinput\" value=\"
$postalcode\" tabindex=\"6\" size=\"18\" /></td> 
            <td>Fax: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"fax\" id = \"textinput\" value=\"
$fax\" tabindex=\"18\" size=\"18\" /></td> 
          </tr> 
          <tr> 
            <td>Country: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"country\" id = \"textinput\" value=\"
$country\" tabindex=\"7\" size=\"18\" /></td> 
            <td>AIM: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"aim\" id = \"textinput\" value=\"\" tabindex=\"19\" size=\"18\" /></td> 
          </tr> 
          <tr> 
            <td>Birth Date (MM/DD/YYYY): 
              <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"bdayd\" id = \"textinput\" value=\"
$bdayd\" tabindex=\"8\" size=\"5\" /><input type=\"text\" name=\"bdaym\" id = \"textinput\" value=\"$bdaym\" tabindex=\"9\" size=\"5\" /><input type=\"text\" name=\"bdayy\" id = \"textinput\" value=\"$bdayy\" tabindex=\"10\" size=\"5\" /></td> 
            <td>Skype: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"skype\" id = \"textinput\" value=\"
$skype\" tabindex=\"20\" size=\"18\" /></td> 
          </tr> 
          <tr> 
            <td>Website:<br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"website\" id = \"website\" value=\"
$website\" tabindex=\"11\" size=\"18\" /></td> 
            <td>GTalk: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"gtalk\" id = \"textinput\" value=\"
$gtalk\" tabindex=\"21\" size=\"18\" /></td> 
          </tr> 
          <tr> 
            <td><table width=\"100%\" border=\"0\"><tr><td>Comments: <br /><div style=\"padding:10px 0 0 20px;\"><textarea name=\"comments\" cols=\"18\" rows=\"8\" id=\"textinput\" tabindex=\"12\" value=\"
$comments\"></textarea></div></td></tr>
            
            <tr><td>Relationship: <br /><input type=\"radio\" name=\"relation\" id=\"option1\" value=\"0\" checked=\"checked\" /><label for=\"option1\">Friend</label><br />
    <input type=\"radio\" name=\"relation\" id=\"option2\" value=\"1\" /><label for=\"option2\">Family</label>    </tr></td>            
            </table></td> 
            <td><table width=\"100%\" border=\"0\"> 
              <tr> 
                <td>Jabber: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"jabber\" id = \"textinput\" value=\"
$jabber\" tabindex=\"22\" size=\"18\" /></td> 
              </tr> 
              <tr> 
                <td>ICQ: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"icq\" id = \"textinput\" value=\"
$icq\" tabindex=\"23\" size=\"18\" /></td> 
              </tr> 
                      <tr> 
                             <td>MSN: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"msn\" id = \"textinput\" value=\"
$msn\" tabindex=\"24\" size=\"18\" /></td> 
                      </tr> 
                      <tr> 
                            <td>Yahoo Messenger: <br />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"yahoo\" id = \"textinput\" value=\"
$yahoo\" tabindex=\"25\" size=\"18\" /></td> 
                    </tr> 
            </table></td> 
          </tr> 
              <tr> 
                    <td align=\"right\"> 
                    <input name=\"Submit\" type=\"submit\" id=\"button\" value=\"Submit\" tabindex=\"26\" /> 
                    <input name=\"Reset\" type=\"reset\" id=\"button\" value=\"Reset\" tabindex=\"27\" /> 
                    </td> 
              </tr> 
        </table> 
            </form> 
                    </div> 
            </div> 
            <div id=\"footer\"> 
                    &copy; 2007 <strong>Ayush Sood</strong> 
            </div> 
    </div> 
    </body> 
    </html>"
); 
}
else
{
    if(
$relation==0){
    
mysql_connect($host,$dbuser,$dbpwd); // Make a MySQL Connection - var imported from config.php
    
mysql_select_db($dbname) or die(mysql_error()); //connect to database
    
$passEN sha1$password );
    
mysql_query("INSERT INTO personal VALUES('','$firstname', '$lastname', '$address', '$postalcode', '$city', '$province', '$country', '$home', '$mobile', '$work', '$fax', '$email', '$email2', '$bdayd', '$bdaym', '$bdayy', '$yahoo', '$msn', '$icq', '$website', '$comments', '$aim', '$skype', '$jabber', '$gtalk') ") or die(mysql_error()); 
     
header("Location: home.php");  
    }
    elseif(
$relation==1){
    
mysql_connect($host,$dbuser,$dbpwd); // Make a MySQL Connection - var imported from config.php
    
mysql_select_db($dbname) or die(mysql_error()); //connect to database
    
$passEN sha1$password );
    
mysql_query("INSERT INTO family VALUES('','$firstname', '$lastname', '$address', '$postalcode', '$city', '$province', '$country', '$home', '$mobile', '$work', '$fax', '$email', '$email2', '$bdayd', '$bdaym', '$bdayy', '$yahoo', '$msn', '$icq', '$website', '$comments', '$aim', '$skype', '$jabber', '$gtalk') ") or die(mysql_error()); 
     
header("Location: home.php");  
    }
}
The first thing that you would notice is the huge bunch of 'if' statements I have in the top of the program. I learned to import variables that way, though have been told that it isn't the best method. How should I fix that?

And is there a built in function in php that can validate this information?

Thanks!
__________________
- sushi
Visit http://napkinz.com/index.php - web comic that is update weekly

-ps: got through the archive...there are really funny comics in there....
Reply With Quote
  #2 (permalink)  
Old 10-05-07, 07:22 PM
Triexa Triexa is offline
Newbie Coder
 
Join Date: May 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Why do you need to put all those $_REQUEST['value'] into $var? Why not just reference the $_REQUEST itself?

Just clean up that section though, you could do something like:
PHP Code:

$fields = array(
    
'firstname' => 'STRING',
    
'lastname' => 'STRING',
    
'icq' => 'INT',
);

foreach (
$fields as $field => $type) {
    if (
$type == 'STRING')
        
$localValue[$field] = strval($_REQUEST['field']);
    else if (
$type == 'INT')
        
$localValue[$field] = intval($_REQUEST['field']);

that also deals with some basic validation though depending on what you want you probably want to create some custom routines to verify the data.
__________________
AskItOnline.com - Need answers? Ask it online.
Create powerful online surveys with ease in minutes!
Sign up for your FREE account today!
Reply With Quote
  #3 (permalink)  
Old 10-20-07, 05:21 PM
sushi4664's Avatar
sushi4664 sushi4664 is offline
Aspiring Coder
 
Join Date: Apr 2007
Location: USA
Posts: 411
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry...I was away for the past long time and was not able to respond to this...

Though the way you have handled it you are explicitly telling what type of variable should be in the array right? Doesn't PHP self assign that?

What I am really asking is:

I know that it is really easy to make "bad code" in php. Is what I did bad? Is it neccessary to change and improve it? And how? I realize a way has been posted above...but is that more efficient or better than what I did?

As in regards to validating...I am interested in some type of "thing" that can help me validate emails and phone numbers and zip codes to make sure that they are not just random things entered by some incompetent user...

thanks again!
__________________
- sushi
Visit http://napkinz.com/index.php - web comic that is update weekly

-ps: got through the archive...there are really funny comics in there....
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Probs with address book sushi4664 PHP 1 08-14-07 08:35 AM
PHP Include not working in some of my templates Tomi PHP 11 01-16-07 03:48 AM
PHP Not Working ProjectJustice PHP 2 06-25-06 07:37 PM
Css and php not working together homeboy PHP 0 12-23-05 04:28 PM
Proven PHP Script not working on Windows 2000 server – any ideas? chimchim PHP 5 11-13-03 09:16 PM


All times are GMT -5. The time now is 06:11 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.