PHP and MySQL Tutorial

Discussion in 'Web Design and Development' started by SC68Cal, Apr 18, 2007.

  1. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #1
    Hey everyone, I'm going to post a sample code that I used while practicing bridging HTML varaibles to PHP variables to a MySQL database using sessions. Critique is welcomed, and if the code is good enough someone is more than welcome to toss it into the Mac Guide (http://guides.macrumors.com/Working_with_PHP_and_MySQL)
     
  2. tutubibi macrumors 6502a

    tutubibi

    Joined:
    Sep 18, 2003
    Location:
    localhost
    #2
    Good idea to capture that knowledge but I would recommend putting a section on how to install PHP if not installed already before configuring Apache. (with link to entropy.ch).

    Also, maybe add a section how to install virtual hosts in httpd? Or that should be separate article?
     
  3. SC68Cal thread starter macrumors 68000

    Joined:
    Feb 23, 2006
    #3
    well, we can address that as well. I think first we should make the guide and assume that they have purchased a hosting plan that supports PHP and MySQL (which is what most people do) and then drill down further into home setup.
     
  4. SC68Cal thread starter macrumors 68000

    Joined:
    Feb 23, 2006
    #4
    okay, first page.

    Code:
    <?php session_start();
    $name=$_SESSION['name'];
    $email=$_SESSION['email'];
    $phone=$_SESSION['phone'];
    $office=$_SESSION['office'];
    $nickname=$_SESSION['nickname'];
    
    echo "Your name is $name <br>";
    echo "Your email is $email <br>";
    echo "Your phone is $phone <br>";
    echo "Your office is $office <br>";
    echo "Your nickname is $nickname <br>";
    ?>
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Page 1</title>
    </head>
    <body>
    	<form method="post" action="pg2.php">
    		<p>Your name?</p><input type="text" name="name" />
    		<p>Your email?</p><input type="text" name="email" />
    		<input type="submit" />
    	</form>
    
    </body>
    </html>
    
     
  5. SC68Cal thread starter macrumors 68000

    Joined:
    Feb 23, 2006
    #5
    page 2

    Code:
    <?php session_start(); ?>
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Page 3</title>
    </head>
    <?php 
    $_SESSION['name']=$_POST['name'];
    $_SESSION['email']=$_POST['email'];
    
    $name=$_SESSION['name'];
    $email=$_SESSION['email'];
    $phone=$_SESSION['phone'];
    $office=$_SESSION['office'];
    $nickname=$_SESSION['nickname'];
    
    echo "Your name is $name <br>";
    echo "Your email is $email <br>";
    echo "Your phone is $phone <br>";
    echo "Your office is $office <br>";
    echo "Your nickname is $nickname <br>";
    ?>
    <body>
    	<form method="post" action="pg3.php">
    		<p>Your telephone?</p><Input type="text" name="phone" />
    		<p>Your office?</p><input type="text" name="office" />
    		<input type="submit" />
    	</form>
    </body>
    </html>
    
     
  6. SC68Cal thread starter macrumors 68000

    Joined:
    Feb 23, 2006
    #6
    page 3

    Code:
    <?php  session_start();?>
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Page 3</title>
    </head>
    <?php
    $_SESSION['phone']=$_POST['phone'];
    $_SESSION['office']=$_POST['office'];
    
    $name=$_SESSION['name'];
    $email=$_SESSION['email'];
    $phone=$_SESSION['phone'];
    $office=$_SESSION['office'];
    $nickname=$_SESSION['nickname'];
    
    echo "Your name is $name <br>";
    echo "Your email is $email <br>";
    echo "Your phone is $phone <br>";
    echo "Your office is $office <br>";
    echo "Your nickname is $nickname <br>";
    ?>
    <body>
    <form method="post" action="pg4.php">
    	<p>What is your nickname?</p><input type="text" name="nickname" />
    	<input type="submit"  />
    </form>
    
    </body>
    </html>
    
     
  7. SC68Cal thread starter macrumors 68000

    Joined:
    Feb 23, 2006
    #7
    page 4

    Code:
    <?php  session_start();?>
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Page 4</title>
    </head>
    <?php
    $_SESSION['nickname']=$_POST['nickname'];
    
     ?>
    <body>
    <?php
    
    //output session variables to confirm
    $name=$_SESSION['name'];
    $email=$_SESSION['email'];
    $phone=$_SESSION['phone'];
    $office=$_SESSION['office'];
    $nickname=$_SESSION['nickname'];
    
    echo "Your name is $name <br>";
    echo "Your email is $email <br>";
    echo "Your phone is $phone <br>";
    echo "Your office is $office <br>";
    echo "Your nickname is $nickname <br>";
    ?>
    <form method="post" action="process.php">
    If this information is correct, press to submit <br />
    <input name="add" type="submit" id="add" value="add" />
    </form>
    
    </body>
    </html>
    
     
  8. SC68Cal thread starter macrumors 68000

    Joined:
    Feb 23, 2006
    #8
    process page, enters it all into the database

     
  9. SC68Cal thread starter macrumors 68000

    Joined:
    Feb 23, 2006
    #9
    this is the actual page that is used to create the database connection, from page 4

    Code:
    <?php # Script - mysql_connect.php
    
    //This file contains the database access information
    //This file also establishes a connection to MySQL and selects the database
    
    //Set the database access information as constants
    DEFINE ('DB_USER', 'youruser'); //has only write and select privs for security
    DEFINE ('DB_PASSWORD', 'yourpassword'); 
    DEFINE ('DB_HOST','yourdbhost'); //database URL
    DEFINE ('DB_NAME','yourdbname'); //selects the database
    
    //Make the connection
    $dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('could not make connection to MySQL: ' . mysql_error());
    
    //Select the database
    @mysql_select_db(DB_NAME) or die ('Could not select the database: ' . mysql_error());
    ?>
     

Share This Page