Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Write a PHP page containing a form with "Username" and "Password" fields
- This page should store submitted information to a MySQL database named "login_info"
- which contains "username" and "password" columns.
- This page should also retreieve the last submitted set of login credentials
- and supply them on the page.
- */
- $link = mysql_connect( 'localhost', 'root', '' );
- if( $link == FALSE )
- {
- die( 'Could not connect: ' . mysql_error() );
- }
- $createDB = 'CREATE DATABASE IF NOT EXISTS login_info';
- if( mysql_query( $createDB, $link ) == FALSE )
- {
- die( 'Could not create server: ' . mysql_error() );
- }
- if( mysql_select_db( 'login_info', $link ) == FALSE )
- {
- die( 'Could not connect to server: ' . mysql_error() );
- }
- $createTable = 'CREATE TABLE IF NOT EXISTS loginInfo
- (
- userID int NOT NULL AUTO_INCREMENT,
- PRIMARY KEY( userID ),
- username varchar( 15 ),
- password varchar( 15 )
- )';
- if( mysql_query( $createTable, $link ) == FALSE )
- {
- die( 'Could not create table: ' . mysql_error() );
- }
- // HTML form created to call this page upon hitting submit.
- $formTemplate = "<form action=\"basicPHPinputForm.php\" method=\"post\">\n
- Username: <input type=\"text\" name=\"username\" />\n
- Password: <input type=\"password\" name=\"password\" />\n
- <input type=\"submit\" />\n
- </form>\n";
- // HTML form printed.
- echo $formTemplate;
- // Variable assignments useless if no input received.
- // $userName = $_POST[ 'username' ];
- // $passWord = $_POST[ 'password' ];
- //If no input received, form stays open and message informs user.
- if( isset( $_POST[ 'username' ] ) == FALSE && isset( $_POST[ 'password' ] ) == FALSE )
- {
- echo "No visitors yet!";
- }
- else
- {
- // Variables initialized to inputs for ease of use and possible filtering later.
- $userName = $_POST[ 'username' ];
- $password = $_POST[ 'password' ];
- // Format for sprintf() to insert login data for mysql_query()
- $insertFormat = "INSERT INTO loginInfo( username, password )
- VALUES
- (
- '%s', '%s'
- )";
- // Login data input and INSERT INTO script formatted appropriately.
- $insertData = sprintf( $insertFormat, $userName, $password );
- // Data inserted into database row, if fails error printed.
- // Otherwise, last successful input printed.
- if( mysql_query( $insertData, $link ) == FALSE )
- {
- die( 'Could not insert data: ' . mysql_error() );
- }
- else
- {
- if( ( $lastUserName = mysql_query( 'SELECT username FROM loginInfo ORDER BY userID DESC LIMIT 1', $link ) ) == FALSE )
- {
- die( 'Username query failed: ' . mysql_error() );
- }
- if( ( $lastPassword = mysql_query( 'SELECT password FROM loginInfo ORDER BY userID DESC LIMIT 1', $link ) ) == FALSE )
- {
- die( 'Password query failed: ' . mysql_error() );
- }
- /*
- if( ( $lastInput = mysql_query( 'SELECT * FROM loginInfo ORDER BY userID DESC', $link ) ) == FALSE )
- {
- die( 'Input query failed: ' . mysql_error() );
- }
- */
- if( ( $lastUN = mysql_fetch_assoc( $lastUserName ) == FALSE ) )
- {
- die( 'Fetch username failed: ' . mysql_error() );
- }
- $lastPW = mysql_fetch_array( $lastPassword );
- /*
- if( ( $lastRecord = mysql_fetch_array( $lastInput ) ) == FALSE )
- {
- die( 'Could not fetch last record: ' . mysql_error() );
- }
- echo "The last data submitted was: <br /><br /> username - " . $lastRecord[ 'username' ]
- . "<br />password - " . $lastRecord[ 'password' ];
- */
- echo "The last data submitted was:<br /><br /> username - " . $lastUN[ 'username' ]
- . "<br />password - " . $lastPW[ 'password' ];
- /*
- echo "The last data submitted was:<br /><br /> username - " . $lastUserName
- . "<br />password - " . $lastPassword;
- */
- }
- }
- ?>
Add Comment
Please, Sign In to add comment