Home  >  Article  >  Backend Development  >  Learn to use PHP to write a simple address book with a database in 30 minutes Page 1/3

Learn to use PHP to write a simple address book with a database in 30 minutes Page 1/3

PHP中文网
PHP中文网Original
2016-07-29 08:35:46856browse

                                                                                 Quote from the opening remarks of the previous article:
              I have not been exposed to PHP for a long time, so if there are any shortcomings, you are welcome to correct me and make everyone laugh.
  This small tutorial is aimed at PHP beginners. It contains the simplest and most basic things, so experts can skip it.
In order to arouse the interest of beginners and get started as soon as possible, what is written here is the simplest and most basic PHP program. I believe that as long as you have a little basic knowledge of PHP, you can learn it in 10 minutes. It doesn't matter if you don't have basic knowledge of PHP. As long as you read it patiently, it won't take more than an hour to learn it.
The purpose of my writing this article is to learn and make progress together with everyone, and then drive the fear of PHP among PHP beginners thousands of miles away, so that everyone can enhance their confidence in learning PHP by themselves. In fact, PHP It is not difficult to learn. As long as you calm down and learn, you will definitely succeed. Okay, after talking so much nonsense, let’s start quickly, or you will be scolded, haha.
[Design Idea]
Implementation: Add, view, modify, delete, these basic functions, to start from simplicity, we will only do these.
[Program Planning]
 Haha, don’t be nervous. We won’t do any software engineering here, do any requirements analysis, draw any flow charts, and other things that make people vomit blood. I will be flattened after a while. Of course, these are necessary for formal development, so be sure to learn about them when you have time.
 input.php ---------- The form used to add contact information (of course this can be done in html, but in order to avoid confusion, just unify it all in PHP format)
 post.php --- ------- Used to process form data
 conn.php ---------- Used to connect to the database
  show.php ---------- Used to display data
  edit .php                                                              edited.php                                                                                                                                                                                                                   Let’s delete the data
[Data Planning]
That is the database design. I wrote it in four words just to make it look better. Do not mind. We won’t do any nonsense modeling here, and we will vomit blood, haha. Okay, let's start. It's a bit painful. I can't take screenshots because I'm not behind the times. I drew an extremely ugly table with characters. I can't help it. It's just at this level. Let's just take a look at it. Haha, cry.
  ____________________________________________________________________________
  | Field| Type  |           |
  | name| varchar(10) | utf8_general_ci | No | |
 | sex | tinyint(1) | No | mobi | varchar(11) | utf8_general_ci | | email | varchar(50) | utf8_general_ci | No | | | addr | varchar(50) | utf8_general_ci No
Regarding the database, I will talk about it roughly Let’s take a look:
 idEveryone is familiar with it, right? A unique identifier for a set of data. For example, if you have an outdated ID, click on your ID and a bunch of information about you will appear. This is the unique identifier.
  int(10) is the data type, representing a 10-digit integer. UNSIGNED means non-negative, and auto_increment is automatically added. Since the ID is set to be added automatically, we don’t need to worry about it after we build it, let it fend for itself, haha.
The name field is used to store names. Note that the field name can be named casually, such as "name" now, but for the convenience of subsequent explanations, please name it after me for now. It is recommended that the naming rule be lowercase letters, and underscores can also be added in the middle. varchar(10) stores Chinese characters, 10 characters should be enough, right? Haha, never mind, just leave it like that. Varcha r and char, the former saves space, the latter saves time... I'm going too far, you should Google these data types yourself. utf8_general_ci, character set, this is very important. You must know what character set your database uses, otherwise a bunch of garbled characters will appear. Speaking of character sets, I strongly recommend everyone to use utf8.
  Sex is used to store gender. Why is the type tinyint(1)? Is 1-digit small integer enough? Of course it is enough. Just imitate the binary system and use 0 to represent female and 1 to represent male. I forgot to mention something just now. Some people may not understand yet. NULL means that it is not empty.
 Mobi It’s very intuitive for me to get the name. Let’s quickly walk through it. mobi is used to store mobile phone numbers, which are of course 11 digits.
 email is used to store emails, 50 characters should be more than enough.
 Addr is used to store correspondence addresses. The universe, earth, country, province, city, township, town, and village are all written in. 50 digits should be enough, haha.
  Then let’s create the database now. It is recommended that you use phpMyadmin to import the following statements in SQL mode, because this is the most convenient:

Copy the code The code is as follows:


CREATE TABLE `addr_list` (
​ ` id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(10) NOT NULL,
`sex` tinyint(1) NOT NULL,
`mobi` varchar(11) NOT NULL,
​`email` varchar( 50) NOT NULL,
 `addr` varchar(50) NOT NULL,
 PRIMARY KEY (`id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;


Of course you use MySQL Command Line Client manual input It's okay, if you're not tired, haha, but one thing to note is to remove all the `` symbols. That's the weird button above the TAB key. Otherwise, accidents may occur.
Okay, let’s wait until everyone has finished the database first. After typing for a long time, my hands are sore and my stomach is so hungry, haha. I'm going to eat first, and then continue after a while. I'm behind by 4 points. I hope it's as soon as possible, so that I can take screenshots, but it's quite interesting to draw tables with characters, haha.
Well, now we will make web pages one by one and write programs one by one:
The revolution has not yet won~ Let’s continue. Need to add something to the above: you need to create a database before importing it!
In order to distinguish and facilitate explanation, I named the database list and the data table addr_list. Let’s write out the complete SQL statement.
 First create the database list:

Copy the codeThe code is as follows:

CREATE DATABASE `list`;


 Then create the data table addr_list:

The code is as follows:

C REATE TABLE `addr_list` (

 id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(10) NOT NULL,
`sex` tinyint(1) NOT NULL,
varchar(11) NOT NULL,
`email ` varchar(50) NOT NULL,
`addr` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

 Okay, Let’s make a web page now+ Write a program:


Current page 1/3 123Next page

The above introduces the 30-minute learning to use PHP to write a simple address book with a database Page 1/3, including the content, I hope it will be helpful to the PHP tutorial Interested friends help.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn