PHP Quick Start Chapter II

Source: Internet
Author: User
Tags character set php file create database

The revolution has not yet won ~ Let's go on. Need to add to the above: to create a database before you can import Oh!

To differentiate and facilitate explanations, I name the database list, name the datasheet addr_list, or write out the complete SQL statement.

First create the Database list:

Copy Content to Clipboard

Code:

  CREATE DATABASE `list` ;

Then create the datasheet addr_list:

Copy Content to Clipboard

Code:

  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 ;

OK, let's do the Web + Write program now:



input.php File Code:

copy content to clipboard

Code:

<form id= "Form1" Name= "Form1" method= "Post" action= "post.php"
  <p> Name: <input name= "name" type= "text" id= "name"/></P>
  <p>
   Sex: <input Type= "Radio" name= "Sex" value= "0"/> Ms.
           <input type= "Radio "Name=" Sex "value=" 1 "Mr./>
  </p>
  <p> Mobile: <input name=" mobi "   type= "text" id= "mobi" &NBSP;&NBSP;/></P>
  <p> mailbox: <input name= "email" type= " Text "id=" email "  /></p>
  <p> Address: <input name=" addr "  type=" Text "id=" addr "  /></p>
  <p>
        <input type=" Submit "Name=" value= "Add"/>
        <input type= "reset" name= "Submit2" value= "rewrite"/& Gt
  </p>
</form>

Description: Because there is only HTML code, you can also save it in HTML format. Don't be fooled by the mess <p></p> Oh, the key word we're looking at is form and input. Form mainly looks at method and Action,method are methods, there are post and get, and so on, you can learn about this by yourself; Action is action, which is the destination page that is worth sending. The popular point is that the point button will run to the page. So, input should be aware of the name and ID, to define some good names. In order to facilitate memory, I have the table single-name and the database field name defined as the same, this is not necessary, but still very necessary. The final attention is sex, don't think crooked, I said is gender. Be sure to note that its name is the same, but the value is different oh. As we have said before, we use 0 for women and 1 for men. This form suggests using tools, such as DW, to be done in a few seconds. With handwritten words, you need to have a certain HTML language base. If you use UTF8, be sure to use tools to save all Web pages as UTF8.



post.php file code: (incomplete)

copy content to clipboard

code

: <?php
//Set form variable
$name   = $_post[' name ';
$sex      = $_post[' sex '] ;
$mobi    = $_post[' mobi '];
$email    = $_post[' email '];
$addr     = $_post[' addr '];

//test whether the value is passed
Echo $name   . ' <br> ';
Echo $sex      . ' <br> ';
Echo $mobi    . ' <br> ';
Echo $email    . ' <br> ';
Echo $addr    . ' <br> ';
?

Description: This is an incomplete post.php code, we first write a few simple scripts to test input.php whether the value successfully uploaded post.php page. "Setting form variables" is not necessary, but it is also essential to avoid accidental conditions, hehe. The variables you see now, such as $name, are the name in the front input.php. For example: <input name= "mobi"   type= "text" id= "mobi"   />   (input.php page), inside the Name= " Mobi ",   is here $mobi   (post.php page) This should be OK? Then run input.php page, casually fill out some information, and then press "Add", if the "add" after the go to the post.php page, and the correct display of your fill in the content, then means that the value is successful, we can then do the net. Tip: Echo is a printout, similar to $name starting with a variable,<br> wrapping. Echo is a language structure, not a function, remember.



OK, the value is successful, so what we're going to do is write the value into the database. Before we improve post.php, we write a conn.php file and call it for some operations on the database. So let's take a look at this rudimentary code:

conn.php file code:

copy content to clipboard

code

: <?php
//Set database variable
$db _host   = ' localhost ';   //database host name, generally localhost
$db _ user   = ' root ';        //database user account, depending on individual circumstances
$db _PASSW = ' 123456 ';   //database user password, depending on personal circumstances
$db _name  = ' list ';         //Database specific name, In the database you just created,

//Connection database
$conn = mysql_connect ($db _host, $db _user, $db _passw) or Die (' Database connection failed! ');

//Set character sets, such as UTF8 and GBK, based on the character set of the database
mysql_query ("Set names ' UTF8 '");

//Selected database
mysql_select_db ($db _name, $conn) or Die (' Database selected failed! ');

//Execute SQL Statement (query)
$result = mysql_query ($sql) or Die (' database query failed! ');
?

Description: This code itself does not have any effect, because it needs to be used frequently, so just to reduce the workload do not have to write it repeatedly, so put it in a file, so you can call at any time. There's no need to explain anything, you just need to change the "Set database variables" and "Set character Sets" section on a personal basis. Don't try to run conn.php This script alone, it's going to go wrong because we haven't set $sql yet.



OK, the basic operation of the database is done. Let's start writing the full version of post.php, hehe.

post.php File Code: (full version)

copy content to clipboard

Code:

<?php
//Set form variable
$name   = $_post[' name ';
$sex      = $_post[' sex '];
$mobi    = $_post[' mobi '];
$email    = $_post[' email '];
$addr     = $_post[' addr '];

//SQL statement to execute (here is the Insert Data feature)
$sql = INSERT INTO ' addr_list '
                (' id ', ' name ', ' sex ', ' mobi ', ' email ', ' addr ')
                VALUES
                (NULL, ' $name ', ' $sex ', ' $mobi ', ' $email ', ' $addr ');

//Call conn.php file for database operations
require (' conn.php ');

//Prompt Operation success Information, note: $result exists in conn.php file, called out
if ($result)
{
        Echo ' Congratulations, successful operation! <p> ';
}

[<a href= show.php > View Address Book </a>] [<a href= ' input.php ' > continue to add </a>]

Description: Finally see the full version of post.php, simple enough? Oh. Here need to pay attention to is the SQL statement in the $sql, if not SQL statements, you can use phpMyAdmin to buckle, specific how to buckle, please you Google a little bit, have time to write another. It's better to learn the SQL syntax. then see $sql,id corresponding NULL, said earlier, the ID after the establishment of the tube, so you can leave a value, and then, the database name field corresponding to the $name variable, and so on can be slightly. Write out the statements you need to execute, and then call conn.php to execute it. It's as simple as that. In order to make the interface a bit more user-friendly, we should add some action tips and related links. So our post.php even finished, oh, very simple. But used to study, should be the pursuit of simple and relatively good, hehe.



OK, now we're going to do show.php, show the page. Nearly half of the work has been completed, and then it's done.

show.php File Code:

copy content to clipboard

Code:

[<a href= "input.php" > continue to add </a>]
<?php
Here is the PHP code
$sql = "SELECT * from ' addr_list '";/SQL statement to execute (here is the browsing Data feature)
Require (' conn.php '); Call conn.php file, perform database operations
?>

<!---here HTML code, create a table--->
<table width= "100%" border= "1" >
<tr>
<th bgcolor= "#CCCCCC" scope= "col" > Name </th>
<th bgcolor= "#CCCCCC" scope= "col" > Sex </th>
<th bgcolor= "#CCCCCC" scope= "col" > Mobile </th>
<th bgcolor= "#CCCCCC" scope= "col" > Mailbox </th>
<th bgcolor= "#CCCCCC" scope= "col" > Address </th>
</tr>


<?php
Here is the PHP code
while ($row = Mysql_fetch_row ($result))//Loop start
{

Determining gender
if ($row [2]==0)
{
$sex = ' lady ';
}
Else
{
$sex = ' Sir ';
}
?>

<!---a looping HTML table with PHP code--->
<tr>
<td><?php echo $row [1];?></td>
<td><?php Echo $sex; ?></td>
<td><?php echo $row [3];?></td>
<td><?php echo $row [4];?></td>
<td><?php echo $row [5];?></td>
</tr>


<?php
}
?>

</table>

Description: This code is extremely ridiculous, because we can not talk about such as Smarty and phplib template engine, so can only be PHP folder with HTML in the same file inside, but the real development of the time can not do so ah, or I will become a ages. The situation is forced and the situation is forced. It is not difficult to understand, seize a principle <?php here is the PHP code?, on the contrary, the outside is the HTML code slightly. Focus on while ($row = Mysql_fetch_row ($result)), where Mysql_fetch_row ($result) gets us to execute the SQL statement to save the result set to an array. Of course you don't need to rush to understand what is called an array, you just need to know, it helps our fields lined up, remember from the 0 row of Oh:0->id;  1->name;  2->sex;   3->mobi;   4->email;   5->addr    So at a glance, we need to call the time with $row[x] to invoke, X is the queue number, such as Call name, is $row[1], because we do not require the display ID, so the above code does not $row[0], then why more $sex? Because we can not use numbers to show the sex bar, so added a conditional sentence: if $row[2] equals 0, then show "Lady", otherwise show "Sir". Of course, you can change the name of the gender, such as 0 show Eve, 1 show Adam, hehe.



OK, so let's get here first. There are two parts left: Modify and delete, and we'll fix them next time. Look fast, write it is not so easy, my hands are almost dislocated, hehe.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.