· 8 years ago · Apr 12, 2018, 04:04 PM
1<?php
2
3
4/* Write a PHP page containing a form with "Username" and "Password" fields
5This page should store submitted information to a MySQL database named "login_info"
6which contains "username" and "password" columns.
7This page should also retreieve the last submitted set of login credentials
8and supply them on the page.
9*/
10
11$link = mysql_connect( 'localhost', 'root', '' );
12
13if( $link == FALSE )
14{
15 die( 'Could not connect: ' . mysql_error() );
16}
17
18$createDB = 'CREATE DATABASE IF NOT EXISTS login_info';
19
20if( mysql_query( $createDB, $link ) == FALSE )
21{
22 die( 'Could not create server: ' . mysql_error() );
23}
24
25if( mysql_select_db( 'login_info', $link ) == FALSE )
26{
27 die( 'Could not connect to server: ' . mysql_error() );
28}
29
30
31$createTable = 'CREATE TABLE IF NOT EXISTS loginInfo
32(
33 userID int NOT NULL AUTO_INCREMENT,
34 PRIMARY KEY( userID ),
35 username varchar( 15 ),
36 password varchar( 15 )
37)';
38
39
40if( mysql_query( $createTable, $link ) == FALSE )
41{
42 die( 'Could not create table: ' . mysql_error() );
43}
44
45// HTML form created to call this page upon hitting submit.
46$formTemplate = "<form action=\"basicPHPinputForm.php\" method=\"post\">\n
47Username: <input type=\"text\" name=\"username\" />\n
48Password: <input type=\"password\" name=\"password\" />\n
49<input type=\"submit\" />\n
50</form>\n";
51
52// HTML form printed.
53echo $formTemplate;
54
55// Variable assignments useless if no input received.
56// $userName = $_POST[ 'username' ];
57// $passWord = $_POST[ 'password' ];
58
59//If no input received, form stays open and message informs user.
60if( isset( $_POST[ 'username' ] ) == FALSE && isset( $_POST[ 'password' ] ) == FALSE )
61{
62 echo "No visitors yet!";
63}
64else
65{
66 // Variables initialized to inputs for ease of use and possible filtering later.
67 $userName = $_POST[ 'username' ];
68 $password = $_POST[ 'password' ];
69
70 // Format for sprintf() to insert login data for mysql_query()
71 $insertFormat = "INSERT INTO loginInfo( username, password )
72 VALUES
73 (
74 '%s', '%s'
75 )";
76
77 // Login data input and INSERT INTO script formatted appropriately.
78 $insertData = sprintf( $insertFormat, $userName, $password );
79
80 // Data inserted into database row, if fails error printed.
81 // Otherwise, last successful input printed.
82 if( mysql_query( $insertData, $link ) == FALSE )
83 {
84 die( 'Could not insert data: ' . mysql_error() );
85 }
86 else
87 {
88
89 if( ( $lastUserName = mysql_query( 'SELECT username FROM loginInfo ORDER BY userID DESC LIMIT 1', $link ) ) == FALSE )
90 {
91 die( 'Username query failed: ' . mysql_error() );
92 }
93
94
95 if( ( $lastPassword = mysql_query( 'SELECT password FROM loginInfo ORDER BY userID DESC LIMIT 1', $link ) ) == FALSE )
96 {
97 die( 'Password query failed: ' . mysql_error() );
98 }
99
100
101 /*
102 if( ( $lastInput = mysql_query( 'SELECT * FROM loginInfo ORDER BY userID DESC', $link ) ) == FALSE )
103 {
104 die( 'Input query failed: ' . mysql_error() );
105 }
106 */
107
108
109 if( ( $lastUN = mysql_fetch_assoc( $lastUserName ) == FALSE ) )
110 {
111 die( 'Fetch username failed: ' . mysql_error() );
112 }
113
114
115 $lastPW = mysql_fetch_array( $lastPassword );
116
117 /*
118 if( ( $lastRecord = mysql_fetch_array( $lastInput ) ) == FALSE )
119 {
120 die( 'Could not fetch last record: ' . mysql_error() );
121 }
122
123
124 echo "The last data submitted was: <br /><br /> username - " . $lastRecord[ 'username' ]
125 . "<br />password - " . $lastRecord[ 'password' ];
126 */
127
128
129 echo "The last data submitted was:<br /><br /> username - " . $lastUN[ 'username' ]
130 . "<br />password - " . $lastPW[ 'password' ];
131
132
133 /*
134 echo "The last data submitted was:<br /><br /> username - " . $lastUserName
135 . "<br />password - " . $lastPassword;
136 */
137
138 }
139}
140
141
142
143
144
145
146?>