note 71695 added to ref.mysql

From: Date: Tue, 12 Dec 2006 09:41:46 +0000
Subject: note 71695 added to ref.mysql
Groups: php.notes 
Request: Send a blank email to php-notes+get-120543@lists.php.net to get a copy of this message
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"); } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 24.63.39.4 ---- Manual Page -- http://www.php.net/manual/en/ref.mysql.php Edit -- https://master.php.net/note/edit/71695 Del: integrated -- https://master.php.net/note/delete/71695/integrated Del: useless -- https://master.php.net/note/delete/71695/useless Del: bad code -- https://master.php.net/note/delete/71695/bad+code Del: spam -- https://master.php.net/note/delete/71695/spam Del: non-english -- https://master.php.net/note/delete/71695/non-english Del: in docs -- https://master.php.net/note/delete/71695/in+docs Del: other reasons-- https://master.php.net/note/delete/71695 Reject -- https://master.php.net/note/reject/71695 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120543) next »