Looking for php form input and display script

Status
Not open for further replies.

linux1880

In Runtime
Messages
455
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.
 
mysql.php
Connect to MySQL database
PHP:
<?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:
<?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:
<?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 />';
}

?>
 
Haha, now you're getting a little complicated. :p

login.php
Create the login page
PHP:
<?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:
<?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.
 
Status
Not open for further replies.
Back
Top Bottom