我对 HTML、PHP 和 MySQL 非常陌生。下面是一个家庭作业项目。
我们正在扩展我们之前做过的一个项目。我们之前的项目用 HTML 和 PHP 创建了一个联系表单,然后将其上传到我们学校的服务器。我将为您提供项目所有文件的代码。
以下是联系表单 (Assignment3.html) 的 .html 文件的代码:
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
</style>
</head>
<body bgcolor="green">
<form action="Registered.php" method="post" align="center">
</br>
</br>
<label>
<font size="5">
<strong>Contact Form</strong>
</font>
</label>
<p>
<strong>First Name:</strong>
<input type="text" name="fname">
</p>
<p>
<strong>Last Name:</strong>
<input type="text" name="lname">
</p>
<p>
<strong>Address:</strong>
<input type="text" name="address">
</p>
<p>
<strong>State:</strong>
<select name="state">
<option value="Al">Al</option>
<option value="AK">AK</option>
<option value="AS">AS</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="DC">DC</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
</select>
</strong></strong></p>
<strong><strong>
<p>
<strong>Zip Code:</strong>
<input type="text" name="zip">
</p>
<p>
<strong>Phone Number:<strong>
<input type="text" name="phone">
</strong></strong></p>
<strong><strong>
<p>
<strong>Email:<strong>
<input type="text" name="email">
</strong></strong></p>
<strong><strong>
<p>
<input type="reset" value="Reset">
<input type="submit" value="Save to database">
<input type="submit" value="Save to file"></p>
<p>
<a href="">View contacts in database</a>
</p>
<p>
<a href="http://web-students.armstrong.edu/~tp2283/contactsFile.html">View contacts in file</a>
</p>
</strong></strong></strong></strong></strong></strong>
</form>
</body>
</html>
这是 .php 文件 (Registered.php) 的代码:
<html>
<head>
<title> Thank You </title>
</head>
<body bgcolor = "blue">
<?php
#declare variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address = $_POST['address'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<h1 align = "center"> Thanks for Registering! </h1>
<p align = "center"> Your information is: </p>
<table align = "center">
<tr>
<td> First Name: </td>
<td>   </td>
<td> <?php echo $fname ?> </td>
</tr>
<tr>
<td> Last Name: </td>
<td>   </td>
<td> <?php echo $lname ?> </td>
</tr>
<tr>
<td> Address: </td>
<td>   </td>
<td> <?php echo $address ?> </td>
</tr>
<tr>
<td> State: </td>
<td>   </td>
<td> <?php echo $state ?> </td>
</tr>
<tr>
<td> Zip: </td>
<td>   </td>
<td> <?php echo $zip ?> </td>
</tr>
<tr>
<td> Telephone: </td>
<td>   </td>
<td> <?php echo $phone ?> </td>
</tr>
<tr>
<td> E-mail: </td>
<td>   </td>
<td> <?php echo $email ?> </td>
</tr>
</table>
<?php
if($_POST['saveToFile'] == 'Save to File') {
$outputstring =
"First Name: $fname
Last Name: $lname
Address: $address
State: $state
Zip: $zip
Telephone: $phone
Email: $email
-----------------------\n";
$fp = fopen("$DOCUMENT_ROOT/../home/students/tp2283/public_html/FormData.txt", 'a');
flock($fp, LOCK_EX);
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
}
?>
<p align="center"><a href="Assignment3.html"> Return to Main Page </a> </p>
</body>
</html>
首先,我会说表单应该保存到包含所有联系人的文本文件中。我不知道该怎么做。我还有一个单独的 .html 文件 (contactsFile.html),它写出联系人。有没有办法将新添加的联系人保存到 .html 文件或至少另一个包含 .html 文件的文本文件?
这是contactsFile.html 代码:
<html>
<head><meta http-equiv = "Content-Type" content="text/html; charset=ISO-8859-1">
<style><type="text/css"></style>
</head>
<body bgcolor = "red"><center>
<p>
<font size="7">
<strong>Contacts:</strong>
</font>
</p>
<p>First name: Don</p>
<p>Last name: Juan</p>
<p>Address: 120 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1111</p>
<p>Email: fake1@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Jim</p>
<p>Last name: Piper</p>
<p>Address: 121 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1112</p>
<p>Email: fake2@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Sarah</p>
<p>Last name: Arnold</p>
<p>Address: 122 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1113</p>
<p>Email: fake3@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Bethany</p>
<p>Last name: Hattaway</p>
<p>Address: 123 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1114</p>
<p>Email: fake4@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Jermaine</p>
<p>Last name: Siler</p>
<p>Address: 124 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1115</p>
<p>Email: fake5@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Michael</p>
<p>Last name: Crosby</p>
<p>Address: 125 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1116</p>
<p>Email: fake6@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Jayme</p>
<p>Last name: Collins</p>
<p>Address: 126 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1117</p>
<p>Email: fake7@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Mitchell</p>
<p>Last name: Hudson</p>
<p>Address: 127 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1118</p>
<p>Email: fake8@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Roz</p>
<p>Last name: Wilson</p>
<p>Address: 128 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1119</p>
<p>Email: fake9@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Laura</p>
<p>Last name: Spangenburg</p>
<p>Address: 129 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1120</p>
<p>Email: fake10@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Morris</p>
<p>Last name: Alfred</p>
<p>Address: 130 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1121</p>
<p>Email: fake21@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Peggy</p>
<p>Last name: Williams</p>
<p>Address: 131 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1122</p>
<p>Email: fake22@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Tyler</p>
<p>Last name: Daniel</p>
<p>Address: 132 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1123</p>
<p>Email: fake23@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Sean</p>
<p>Last name: Michaels</p>
<p>Address: 133 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1124</p>
<p>Email: fake24@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Jim</p>
<p>Last name: Heart</p>
<p>Address: 134 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1251</p>
<p>Email: fake25@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Paul</p>
<p>Last name: Bernard</p>
<p>Address: 135 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1126</p>
<p>Email: fake26@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Jessica</p>
<p>Last name: Simpson</p>
<p>Address: 121 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1142</p>
<p>Email: fake42@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Darren</p>
<p>Last name:Lockheart</p>
<p>Address: 142 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1162</p>
<p>Email: fake46@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Michael</p>
<p>Last name: Scott</p>
<p>Address: 1121 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1133</p>
<p>Email: fake33@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Donald</p>
<p>Last name: Duck</p>
<p>Address: QUACK Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-5555</p>
<p>Email: fake555@yahoo.com</p>
<p>_____________________________________________</p>
</center>
<p align="center"><a href="Assignment3.html"> Return to Main Page </a> </p>
</body></html>
为了扩展任务,我们应该使用相同的应用程序,但这次我们要创建一个 MySQL 数据库来存储来自表单的信息。
这是我没有经验的事情。我对 HTML 和一些 PHP 没问题,但不知道如何转换为 MySQL 或如何去做?
这是我的表格的图像;除了“保存到数据库”和“查看数据库中的联系人”以及将新联系人保存到文件之外,一切正常。
http://i44.tinypic.com/2dqmv7q.jpg
接下来,我们应该编写一个 SQL 脚本,您可以运行该脚本并生成并为应用程序填充至少 100 个联系人的适当表:
请为您的数据库中的表(或表)的每个字段提供简要说明。
在这一切都完成后,您将完成对 Web 应用程序采取的安全措施。