Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Looking for php form input and display script
Closed Thread
Old 05-30-2008, 04:01 AM   #1 (permalink)
 
Super Techie

Join Date: Feb 2004

Posts: 442

linux1880 is on a distinguished road

Send a message via Yahoo to linux1880
Default Looking for php form input and display script

I am looking for a simple php script, where I can fill up the form in one page and display it on the another page from database. Is there any reaymade script like that or any software I can buy ?

Please let me know thank you.
linux1880 is offline  
Old 05-31-2008, 01:48 AM   #2 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Looking for php form input and display script

mysql.php
Connect to MySQL database
PHP Code:
<?php
$host 
'localhost';
$user 'root';
$pass 'root';
$dbname 'database_name';

$connect = @mysql_connect ($host,$user,$pass) or die ('error connecting to database');
$select  = @mysql_select_db ($dbname) or die ('error selecting database');

?>
insert.php
Send data to database
PHP Code:
<?php
require_once ('mysql.php'); // connect to database

if (isset ($_POST['submit'])) { // check if form has been sent
    // now get the form values

    
$field1 $_POST['field1'];
    
$field2 $_POST['field2'];

    
// insert them to database with SQL query

    
$sql mysql_query ("INSERT INTO tablename (field1, field2) VALUES ('".$field1."', '".$field2."')");
    echo 
'inserted';
} else { 
// the form was not sent, so display it
    
echo '<form action="insert.php" method="post">
    Field 1 <input type="text" name="field1" /><br />
    Field 2 <input type="text" name="field2" /><br />
    <input type="submit" name="submit" value="Submit" />
    </form>'
;
}
    
?>
view.php
View the data from database
PHP Code:
<?php
require_once ('mysql.php'); // connect to database

// select data from database with SQL query

$sql mysql_query ("SELECT * FROM tablename");

// view with foreach or while loop

echo 'Foreach loop:<br />';
foreach (
mysql_fetch_array ($sql) as $row) {
    echo 
'Field 1:'.$row['field1'].'<br />';
    echo 
'Field 2:'.$row['field2'].'<br />';
}

echo 
'While loop:<br />';
while (
$row mysql_fetch_array ($sql)) {
    echo 
'Field 1:'.$row['field1'].'<br />';
    echo 
'Field 2:'.$row['field2'].'<br />';
}

?>

__________________

Need website help? PM me!

Last edited by CrazeD; 05-31-2008 at 01:50 AM.
CrazeD is offline  
Old 06-03-2008, 12:09 PM   #3 (permalink)
 
Super Techie

Join Date: Feb 2004

Posts: 442

linux1880 is on a distinguished road

Send a message via Yahoo to linux1880
Default Re: Looking for php form input and display script

Thank you. Can we add, login system on it ?
linux1880 is offline  
Old 06-03-2008, 10:23 PM   #4 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Looking for php form input and display script

Haha, now you're getting a little complicated.

login.php
Create the login page
PHP Code:
<?php
require_once ('mysql.php'); // connect to database
session_start ();

if (
$_SESSION['loggedin'] != true) {
    if (isset (
$_POST['submit'])) { // check if form has been sent
        // now get the form values

        
$username $_POST['username'];
        
$password $_POST['password'];

        
// see if they match a database record

        
$sql mysql_query ("SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1");
        if (
mysql_num_rows ($sql) > 0) {
            
// a record is found, so set some sessions to log them in

            
$_SESSION['username'] = $username;
            
$_SESSION['loggedin'] = true;
        } else {
            echo 
'Username/Password not found, please try again!';
        }    
    } else { 
// the form was not sent, so display it
        
echo '<form action="login.php" method="post">
        Username <input type="text" name="username" /><br />
        Password <input type="text" name="password" /><br />
        <input type="submit" name="submit" value="Submit" />
        </form>'
;
    }
} else {
    echo 
'You are already logged in!';
}

?>
logout.php
Create the logout page
PHP Code:
<?php
session_start 
();

unset (
$_SESSION);
session_destroy ();
?>
Any page that shall require login, use a if else statement like this:

if ($_SESSION['loggedin'] == true) {
// they're logged in, so they can see this page
} else {
// they're NOT logged in, they CAN'T see this page
}

This is an extremely basic login script, and not secure whatsoever. You should use MD5 or SHA1 with a salt to encrypt the password, and use mysql_escape_string () to escape potentially malicious users from hacking your website.
__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sound Card Tips EricB Computer Audio & Multimedia 2 02-18-2008 01:37 AM