MySQL for Linux on POWER 开发应用
2008-01-18 11:06:14 来源:WEB开发网这个表单的主要组件是:文本输入框 "Personal_FirstName" 和 "Personal_LastName",三个用于输入雇员的 SSN 的文本框,以及具有工作名称和相应分配的工作代码的下拉单选列表,见下面摘录的代码:
. . .
<SELECT NAME="Job_Title">
<option value="200" selected>Application Programmer</option>
<option value="201">Database Administrator</option>
<option value="202">Technical Support</option>
<option value="203">Database Designer</option>
</SELECT>
. . .
注意:为了重点介绍主要的概念,此示例并没有引入严格的用户数据输入验证和错误处理,这在生产质量的应用程序中通常是会有的。
当用户点击 Submit Form 按钮时,下面所示的 insert.php 脚本就会在服务器端运行:
清单 1. PHP 代码示例
<?
//MySQL server username, password, and database name
$username="username";
$password="password";
$database="CONTRACTING";
//Extracting information from the form
$first_name=$_POST['Personal_FirstName'];
$last_name=$_POST['Personal_LastName'];
$ssn=$_POST['Personal_SSN1'].$_POST['Personal_SSN2'].$_POST['Personal_SSN3'];
$job_id=$_POST['Job_Title'];
echo '<b>', 'Data extracted from the form: ', '</b>', <br>';
echo 'First Name: ', "$first_name", '<br>';
echo 'Last Name: ', "$last_name", '<br>';
echo 'Social Security Number: ', "$ssn", '<br>';
echo 'Job ID: ', "$job_id", '<p>';
//Connecting to the MySQL server
echo '<b>', 'MySQL server connection status: ', '</b>';
$conn=mysql_connect('127.0.0.1',$username,$password);
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully', '<p>';
//Changing into the database
echo '<b>', 'Selecting database ', "$database", ' status: ', '</b>';
@mysql_select_db($database) or die( "Unable to select database");
echo 'Successful', '<p>';
//Inserting data into the database
$query = "INSERT INTO EMPLOYEE VALUES ('$ssn','$last_name','$first_name','$job_id')";
echo '<b>', 'SQL query: ', '</b>', "$query", '<p>';
echo '<b>', 'Inserting data status: ', '</b>';
$result=mysql_query($query);
if (!$result) {
die(mysql_error());
}
echo 'Successful', '<p>';
//Displaying inserted data
$query="SELECT * FROM EMPLOYEE";
$result=mysql_query($query);
if (!$result) {
die(mysql_error());
}
$num=mysql_numrows($result);
mysql_close($conn);
echo "<b><center>Displaying the EMPLOYEE table contents:</center></b><br><br>";
$i=0;
while ($i < $num) {
$first_name=mysql_result($result,$i,"EMP_FNAME");
$last_name=mysql_result($result,$i,"EMP_LNAME");
$ssn=mysql_result($result,$i,"EMP_SSN");
$job_id=mysql_result($result,$i,"JOB_CODE");
echo "<b>$first_name $last_name</b><br>SSN: $ssn<br>Job ID: $job_id<p>";
$i++;
}
?>
更多精彩
赞助商链接