简单通讯录程序_简单通讯录

简单通讯录程序

This tutorial will walk you through creating a simple address book using PHP and MySQL.

本教程将引导您逐步使用PHP和MySQL创建一个简单的地址簿。

Before you can begin you need to decide what fields you wish to include in our address book. For this demonstration, we will use Name, E-mail and Phone number, although you can modify it to include more options if you like.

开始之前,您需要确定希望在我们的地址簿中包括哪些字段。 对于此演示,我们将使用“姓名”,“电子邮件”和“电话号码”,但是您可以根据需要对其进行修改以包括更多选项。

数据库 ( The Database )

To create this database you need to execute this code:

要创建此数据库,您需要执行以下代码:

CREATE TABLE address (id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30), phone VARCHAR(30), email VARCHAR(30));
INSERT INTO address (name, phone, email) VALUES ( "Alexa", "430-555-2252", "sunshine@fakeaddress.com"), ( "Devie", "658-555-5985", "potato@monkey.us" )

This creates our database fields and puts in a couple of temporary entries for you to work with. You are creating four fields. The first is a ​self incrementing number, then name, phone and email. You will use the number as a unique ID for each entry when editing or deleting.

这将创建我们的数据库字段,并放入几个临时条目供您使用。 您正在创建四个字段。 首先是一个自增数字,然后是姓名,电话和电子邮件。 编辑或删除时,您将使用数字作为每个条目的唯一ID。

连接到数据库 ( Connect to the Database )

<html>
<head>
<title>Address Book</title>
</head>
<body>

<?php // Connects to your Database mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); mysql_select_db("address") or die(mysql_error());

<?php // Connects to your Database mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); mysql_select_db("address") or die(mysql_error());

Before you can do anything, you need to connect to the database. We have also included an HTML title for the address book. Be sure to replace your host address, username, and password with the appropriate values for your server.

在执行任何操作之前,您需要连接到数据库 。 我们还包括了通讯录HTML标题。 确保将主机地址,用户名和密码替换为服务器的适当值。

添加联系人 ( Add a Contact )

if ( $mode=="add")
{
Print '<h2>Add Contact</h2>
<p>
<form action=';
echo$PHP_SELF; 
Print '
method=post>
<table>
<tr><td>Name:</td><td><input type="text" name="name" /></td></tr>
<tr><td>Phone:</td><td><input type="text" name="phone" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" /></td></tr>
<input type=hidden name=mode value=added>
</table>
</form> <p>';
}
if ( $mode=="added")
{
mysql_query ("INSERT INTO address (name, phone, email) VALUES ('$name', '$phone', '$email')");
}

Next, we'll give the users an opportunity to ​add data. Since you are using the same PHP page to do everything, you will make it so that different 'modes' show different options. You would place this code directly under that in our last step. This would create a form to add data, when in add mode. When submitted the form sets the script into added mode which actually writes the data to the database.

接下来,我们会给用户一个机会来添加数据 。 由于您使用相同PHP页面来完成所有操作,因此您将使不同的“模式”显示不同的选项。 您将在我们的最后一步中将此代码直接放在代码下面。 在添加模式下,这将创建一个表单以添加数据。 提交表单后,脚本会将脚本设置为添加模式,该模式实际上会将数据写入数据库。

更新数据 ( Updating Data )

if ( $mode=="edit")
{
Print '<h2>Edit Contact</h2>
<p>
<form action=';
echo $PHP_SELF;
Print '
method=post>
<table>
<tr><td>Name:</td><td><input type="text" value="';
Print $name;
print '" name="name" /></td></tr>
<tr><td>Phone:</td><td><input type="text" value="';
Print $phone;
print '" name="phone" /></td></tr>
<tr><td>Email:</td><td><input type="text" value="';
Print $email;
print '" name="email" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" /></td></tr>
<input type=hidden name=mode value=edited>
<input type=hidden name=id value=';
Print $id;
print '>
</table>
</form> <p>';
}
if ( $mode=="edited")
{
mysql_query ("UPDATE address SET name = '$name', phone = '$phone', email = '$email' WHERE id = $id");
Print "Data Updated!<p>";
}

The edit mode is similar to the add mode except it pre-populates the fields with the data you are updating. The main difference is that it passes the data to the edited mode, which instead of writing new data overwrites old data using the WHERE clause to make sure it only overwrites for the appropriate ID.

编辑模式类似于添加模式,不同之处在于它使用要更新的数据预填充字段。 主要区别在于它将数据传递给已编辑模式,该模式不是写入新数据,而是使用WHERE子句覆盖旧数据,以确保仅覆盖适当的ID。

删除资料 ( Removing Data )

if ( $mode=="remove")
{
mysql_query ("DELETE FROM address where id=$id");
Print "Entry has been removed <p>";
}

To remove data we simply query the database to remove all the data related to the entries ID.

删除数据,我们只需查询数据库即可删除与条目ID相关的所有数据。

通讯录 ( The Address Book )

$data = mysql_query("SELECT * FROM address ORDER BY name ASC")
or die(mysql_error());
Print "<h2>Address Book</h2><p>";
Print "<table border cellpadding=3>";
Print "<tr><th width=100>Name</th><th width=100>Phone</th><th width=200>Email</th><th width=100 colspan=2>Admin</th></tr>"; Print "<td colspan=5 align=right><a href=" .$_SERVER[’PHP_SELF’]. "?mode=add>Add Contact</a></td>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr><td>".$info['name'] . "</td> ";
Print "<td>".$info['phone'] . "</td> ";
Print "<td> <a href=mailto:".$info['email'] . ">" .$info['email'] . "</a></td>";
Print "<td><a href=" .$_SERVER[’PHP_SELF’]. "?id=" . $info['id'] ."&name=" . $info['name'] . "&phone=" . $info['phone'] ."&email=" . $info['email'] . "&mode=edit>Edit</a></td>"; Print "<td><a href=" .$_SERVER[’PHP_SELF’]. "?id=" . $info['id'] ."&mode=remove>Remove</a></td></tr>";
}
Print "</table>";
?>
</body>
</html>

The bottom part of the script actually pulls the data from the database, puts it into an array, and prints it out. Using the PHP_SELF function with actual database data, we are able to link to add mode, edit mode, and remove mode. We pass the appropriate variables within each link, to let the script know which mode is needed.

脚本的底部实际上是从数据库中提取数据,将其放入数组中并打印出来。 使用PHP_SELF函数和实际的数据库数据,我们可以链接到添加模式,编辑模式和删除模式。 我们在每个链接中传递适当的变量,以使脚本知道需要哪种模式。

From here you can make aesthetic changes to this script, or try adding more fields.

在这里,您可以对该脚本进行美学上的更改,或尝试添加更多字段。

You can download the full working code from GitHub.

您可以从GitHub下载完整的工作代码

翻译自: https://www.thoughtco.com/simple-address-book-2693840

简单通讯录程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值