note 71695 deleted from ref.mysql by nlopess
| From: | nlopess@php.net | Date: | Mon, 18 Dec 2006 11:16:38 +0000 |
| Subject: | note 71695 deleted from ref.mysql by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-120732@lists.php.net to get a copy of this message | ||
Note Submitter: brian at glamourati dot com
----
Here is a complete real world PHP MySQL example which displays a form, connects to a database,
inserts form fields, and redirects. Although this code was automatically generated from a PHP MySQL
framework, http://www.wizzyweb.com it is pretty much the same
as what would be written from scratch. I found it useful as it shows how PHP MySQL functions are
actually used in a complete application:
<?php
// DESCRIPTION
// Displays Form, Inserts Form data into Database Table and Redirects
$EmployeeID = $_REQUEST["EmployeeID"];
$EmployeeFirstName = $_REQUEST["EmployeeFirstName"];
$EmployeeLastName = $_REQUEST["EmployeeLastName"];
if ($_SERVER['REQUEST_METHOD'] == 'GET'){
echo '<TABLE WIDTH=450 BORDER=0 CELLSPACING=0 CELLPADDING=3>
<TR BGCOLOR=#OOOO84>
<TD COLSPAN=2><STRONG><FONT COLOR=#FFFFFF> Form</FONT></TD>
</TR>
<FORM METHOD=POST>
<TR VALIGN=TOP BGCOLOR=#BFBFBF>
<TD ALIGN=RIGHT> EmployeeID</TD>
<TD><INPUT TYPE=TEXT NAME=EmployeeID SIZE=40 MAXLENGTH=256 VALUE=></TD>
</TR>
<TR VALIGN=TOP BGCOLOR=#BFBFBF>
<TD ALIGN=RIGHT> EmployeeFirstName</TD>
<TD><INPUT TYPE=TEXT NAME=EmployeeFirstName SIZE=40 MAXLENGTH=256 VALUE=></TD>
</TR>
<TR VALIGN=TOP BGCOLOR=#BFBFBF>
<TD ALIGN=RIGHT> EmployeeLastName</TD>
<TD><INPUT TYPE=TEXT NAME=EmployeeLastName SIZE=40 MAXLENGTH=256 VALUE=></TD>
</TR>
<TR ALIGN=CENTER BGCOLOR=#BFBFBF>
<TD COLSPAN=2><INPUT TYPE=SUBMIT VALUE=Submit> <INPUT TYPE=RESET
VALUE=Reset></TD>
</TR>
</FORM>
</TABLE>';
} else {
$link = mysql_connect('localhost', 'wizzyweb', 'password') or
die('Could not connect: ' . mysql_error());
mysql_select_db('wizzyweb') or die('Could not select database');
$query = "INSERT INTO Employees (EmployeeFirstName, EmployeeLastName) VALUES
('$EmployeeFirstName', '$EmployeeLastName');";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Closing connection
mysql_close($link);
header("Location: http://www.wizzyweb.com");
}
?>