1. You are viewing our forum as a guest. For full access please Register. WindowsBBS.com is completely free, paid for by advertisers and donations.

Resolved Database BBS

Discussion in 'Other PC Software' started by DPI Graphics, 2011/04/08.

  1. 2011/04/08
    DPI Graphics

    DPI Graphics Well-Known Member Thread Starter

    Joined:
    2009/06/12
    Messages:
    283
    Likes Received:
    0
    Does anyone know of a BBS that would have info about databases and HTML web sites? DPI
     
  2. 2011/04/08
    TonyT

    TonyT SuperGeek Staff

    Joined:
    2002/01/18
    Messages:
    9,072
    Likes Received:
    400
    What info are you looking for?
    We have members here that have answers about these subjects.
     

  3. to hide this advert.

  4. 2011/04/08
    DPI Graphics

    DPI Graphics Well-Known Member Thread Starter

    Joined:
    2009/06/12
    Messages:
    283
    Likes Received:
    0
    I need to know the syntax/scripts needed to get/put data to the tables from a web site that is built with HTML. I build web sites with HTML and I used to be a DB2 Database administrator so I know SQL. My problem is I cant figure out how to get there from here and back(the interface between HTML and the database).
     
  5. 2011/04/09
    TonyT

    TonyT SuperGeek Staff

    Joined:
    2002/01/18
    Messages:
    9,072
    Likes Received:
    400
    You cannot do it with HTML.
    You need to code the pages using PHP or another scripting language.
    A PHP page can contain HTML code too. When using PHP, you escape PHP to switch to HTML. The Web server must support PHP.

    I slapped together an example PHP Webpage that inserts a member name into a mysql database called "team ":

    Code:
    <!DOCTYPE HTML PUBLIC  "-//W3C//DTD HTML 4.01 Transitional//EN ">
    <html>
    <head>
    <title>New Member</title>
    </head>
    <body>
    
    <?php
    if ($_POST['submit'] == TRUE) { [COLOR="Red"]// if submit button pressed process form data[/COLOR]
    [COLOR="Red"]
    // get posted form values into local variables[/COLOR]
    $first	= $_POST['first'];
    $last	= $_POST['last'];
    
    [COLOR="Red"]// db connection info[/COLOR]
    $username= "user "; 
    $password= "xyz "; 
    $database= "team ";
    
    [COLOR="Red"]//database connect error message[/COLOR]
    $db_error=  "Unable to connect to database. ";
    
    [COLOR="Red"]// connect to db[/COLOR]
    mysql_connect(localhost,$username,$password); 
    @mysql_select_db($database) or die( $db_error);
    
    [COLOR="Red"]// insert data into members table (id is auto-incremnented)[/COLOR]
    $query =  "INSERT INTO members (id,first,last) VALUES (NULL,'$first','$last') ";
    mysql_query($query);
    [COLOR="Red"]
    // fetch last inserted record & display it with HTML[/COLOR]
    $query =  "SELECT * FROM members order by user_id DESC LIMIT 1 ";
    $result = mysql_query($query);
    mysql_close();
    
    	while($r=mysql_fetEdit Messagech_array($result)) {
    	$id = $myrow[ "id"];
    	$first = $myrow[ "first"];
    	$last = $myrow[ "last"];
    
    	[COLOR="Red"]// escape PHP and switch to HTML using this PHP close tag:[/COLOR] ?>
    	
    	<p>ID: <?php echo $id ?> Name: <?php echo $first ?> <?php echo $last ?></p>
    
    	} [COLOR="Red"]//end while[/COLOR]
    
    } [COLOR="Red"]// end if submit button pressed[/COLOR]
    
    
    
    else { [COLOR="Red"]// if submit not pressed[/COLOR]
    [COLOR="Red"]// escape PHP and switch to HTML using this PHP close tag:[/COLOR] ?>
    
    <form name= "member" method= "post" action= "<?php echo $PHP_SELF;?> ">
    <p>First Name: <input type= "text" name= "first" size= "30" value=" " /><br>
    Last Name: <input type= "text" name= "last" size= "30" value=" " /><br>
    <input type= "submit" name= "submit" value= "Submit "></p>
    </form>
    </body>
    </html>
    
    <?php
    } [COLOR="Red"]// end if submit not pressed[/COLOR]
    ?>
    
    Look here for basic PHP-MYSQL examples:
    http://www.itc.virginia.edu/desktop/web/database/inventory/display/home.html
     
    DPI Graphics likes this.
  6. 2011/04/09
    DPI Graphics

    DPI Graphics Well-Known Member Thread Starter

    Joined:
    2009/06/12
    Messages:
    283
    Likes Received:
    0
    Thank you so much. This should get me what I need to make my application work. DPI.
     
  7. 2011/04/09
    TonyT

    TonyT SuperGeek Staff

    Joined:
    2002/01/18
    Messages:
    9,072
    Likes Received:
    400
    Realize that my code above is not the best as far as security goes. Ideally, you want a separate PHP file that holds the db connection code. Then use the PHP include function to to use the connection code in the page. Also, you only then have to write the connection code once and it can be reused on other pages.

    Store the file "db-connect.php" in a dit outside the public_html dir on the server, a dir that's not publicly accessable. For example, on a linux server, the path to your Website will look like this:

    /home/username/public_html/index.html

    Make a dir in public_html called inc (for includes) and put an empty file there called index.html. Use this dir to store your php include files, such as the db connection script.

    db-connect.php would look like this:
    Code:
    <?php
    [COLOR="Red"]// db connection info[/COLOR]
    $username= "user "; 
    $password= "xyz "; 
    $database= "team ";
    [COLOR="Red"]
    //database connect error message[/COLOR]
    $db_error=  "Unable to connect to database. ";
    
    [COLOR="Red"]// connect to db[/COLOR]
    mysql_connect(localhost,$username,$password); 
    @mysql_select_db($database) or die( $db_error);
    ?>
    Then use it in earlier example I made like this:
    Code:
    // get posted form values into local variables[/COLOR]
    
    $first	= $_POST['first'];
    $last	= $_POST['last'];
    
    include ( "./inc/db-connect.php ");
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.