通信录模块实现

此通信录模块主要功能:1.向mysql数据库中插入通讯录;2.从数据库中读取通讯录并显示。截图如下:

代码如下:

<body bgcolor="#99CCFF" text-align="center">

<form action="index.php" method="post" name="Form">
<table>
<tr>
<td align="right">姓名:</td>
<td><input type="text" size="40" name="firstName" value="" maxlength="50"/></td>
</tr>
<tr>
<td align="right">Email:</td>
<td><input type="text" size="40" name="email" value="" maxlength="100"/></td>
</tr>
<tr>
<td align="right">备注:</td>
<td><textarea name="comments" rows="4" cols="40" wrap="virtual"></textarea></td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" value="添加"/> <input type="reset" value="清除"/></td>
</tr>
</table>
</form>
<br>
<?php
$connect = mysql_connect('localhost', 'root', 'root');
if (!$connect){
die("could not connet:".mysql_error());
}
mysql_select_db('guestbook', $connect);
if (!empty($_POST['firstName'])){
$firstName = $_POST['firstName'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$sql = "insert into guestbook (firstName, email, comments, postDate)values('$firstName', '$email', '$comments', Now());";
mysql_query($sql);
}
$result = mysql_query("select * from guestbook");
if (!$result){
die('Could not query'.mysql_error());
}
?>
<br />
<hr size=2 width=80%/>
<br />
<?php
$num_rows = mysql_num_rows($result);
if ($num_rows > 0){
$array = mysql_fetch_array($result);
while ($array){
?>
<table>
<tr>
<td align="right">姓名</td>
<td align="left"><?php echo $array[1] ?></td>
</tr>
<td align="right">Email</td>
<td align='left'><?php echo $array[2] ?></td>
<tr>
<td align="right">备注</td>
<td align="left"><?php echo $array[3]?></td>
</tr>
<tr>
<td align="right">日期</td>
<td align="left"><?php echo $array[4] ?></td>
</tr>
</table>
<br />
<hr size = 2 width=80%/>
<br />
<?php
$array = mysql_fetch_array($result);
}
}else{
echo "<B><center>Sorry,没有读取数据库记录</B>";
}
mysql_free_result($result);
?>
</body>

 

posted @ 2012-03-29 09:53  icamel  阅读(224)  评论(0编辑  收藏  举报