Getting started with Mysql and php dynamic Website Development

Source: Internet
Author: User
Tags php dynamic website
Mysql and php dynamic website development getting started. this series of tutorials starts from the form, because the form can make the interaction between the database and the web very obvious. Submit a form to record registration information in the database.

This tutorial is a basic tutorial. Please skip this step.

It is very popular with programmers for the stability between php and mysql. Although the hack language has recently been used to replace the php language, it is also based on php. later we can learn the hack language.

It seems that there are a lot of big cows in the blog Park. I don't know if it will be sprayed, so I'm afraid ........ Of course, I have recorded the learning process by myself. The tutorial involves php and mysql, which are simple html forms. use the simplest form to understand the interaction between them.

I remember that when we don't know anything or the code is unclear, we are thinking about the fantastic world of code, and it feels amazing. when we step into the world of code, we were lost again. What is the code. Here we will start with html. Of course, if html doesn't understand it, it won't come to the blog Park. Right-click the browser, and the code for viewing the source code is html. However, it is only the structure of a webpage. to form a beautiful webpage, there will certainly be a lot of things. others will have the opportunity to talk about the interaction between webpages this time. That is, the development of dynamic websites.

If you do not know the form, search for it. That's the box at registration.

The following is the form code.

 

This is the string of code. after the code is saved, the webpage is opened and there is only a box, and clicking is useless. of course, this is the front-end. The backend we are talking about today. What you need is to transfer the input part to the so-called database to save it, so as to facilitate website management. How do I transfer the data? Here we will talk about php and mysql. Note: when running php and mysql, an environment is required. They must have the basis for running. How can they survive. Right? Then there is a famous package. Including these environments. The next step is to install it.

Address: http://apmserv.s135.com/respect the original address.

It seems that windows 8 cannot be installed. Ah...

Install it. familiarize yourself with it first. ~~

Php is in the code area Write php code in the middle. the file is in. php format and can be added to the html language. Well, put the form in.

It is the same as before. Then let it become dynamic. Add several variables.

please type your first name:

please type your second name:

please type your age:

Well, familiar forms. Then you need to include the input in the variable. The php array can be used as a string array. That is, array ['name'] and name can be something you have defined. The form is very special here. this REQUEST is a super global variable and looks cool. it doesn't need to be defined, and it is always there, that is, the form name is put in it and the input

The content is saved in this array. of course, you can also use $ _ POST ['name']. this Post is a subset of the REQUEST. In this case, they are all the same.

Then your data is saved to this array, and it is very convenient to extract it. in the middle, it is used to determine whether the data is filled in, and then return the isset () is to judge whether the data is filled, and then it is the same as the html form. You have to determine whether the data is filled out. you must make a decision.

The following section is available.

$error=array();if (empty($_REQUEST['first_name'])) {echo '

you forget type your first_name;

' ;# code...$error='1';}else{$f=$_REQUEST['first_name'];}if (empty($_REQUEST['second_name'])) {echo '

you forget type your second_name;

' ;# code...$error='2';}else{$s=$_REQUEST['second_name'];}if (empty($_REQUEST['age'])) {echo '

you forget type your age;

'; # code...$error='3';}else{$a=$_REQUEST['age'];}

This empty () is to judge whether it is an empty representation, and echo is the output of php. Html can also be output. Define an error array

Is to judge whether there are any errors, if all are filled in. OK.

Then save those variables to other variables. This looks simple.

Well, then, no error is found.

Then return to this section. well, this section is the most important part. it is used to connect to our database.

if (empty($error)) { echo'

everything is ok

'; require('connect/mysqli_connect.php'); $q="insert into user (first_name,second_name,age) value('$f','$s','$a')"; $r=@mysqli_query($dbc,$q);}

The require () is the file to connect to the database. Database address, table name, user name, password, and contact the database.

This file is changed. just call it to connect. It is require.

This is mysqli_connect.php.

 

Make it as simple as possible. for a very short file, the file is saved in index. in the connect folder under the same directory of php. there is a function mysqli_connect (), which is used to connect to the database.

Mysqli_connect (hostname, username, password, databasename)

The data address names are generally localhost, and of course not necessarily. Check where your database is. Then there is the account, password, and database name.

Then, the returned result is saved in a variable. OK, the call will be connected to the database.

The following defines another $ q, which introduces the knowledge of the database. in the database, you need to create a database, table name, and column, then, the variables are transmitted one by one.

Well, let's talk about the database. ~~ Ah ~~.

Database management is now a lot of things like phpadmin, and it seems very convenient. There is an SQL command in that stuff. you need to enter a small command to create the stuff you just mentioned.

Create a database first. The input is

Create database test;

This creates a database. Test is the database name. It looks simple.

The table name is created below. Or use create

create table user(first_name varchar(20) not null,second_name varchar(20) not null,age varchar(20) not null);

Be sure to pay attention to some semicolons, colons, and commas when writing programs. Note the difference between Chinese and English; otherwise, an error occurs.

Create the table name and column name.

For this Dongdong varchar (20), you need to check the mysql data type. It defines what type of data it is. the column name is first defined.

In this way, a user table is created with three column names first_name second_name age;

Okay, data is transferred.

 $q="insert into user (first_name,second_name,age) value('$f','$s','$a')";

Insert into user (,) value (,,,,,);

Insert en to insert the data. One by one corresponds to the column name followed by its value. Isn't the variable just saved? well. The input stuff goes in.

$r=@mysqli_query($dbc,$q);

This function is also important. The connection before mysqli_query () is followed by the query parameter.

In this way, the connection is established. Doon, Doon, and ~~~ It is running ..

Enter something to the form. Click submit and refresh the database to see if the column name is missing.

All the code is the main index. php below.

 this is a test

';$error=array();if (empty($_REQUEST['first_name'])) {echo '

you forget type your first_name;

' ;# code...$error='1';}else{$f=$_REQUEST['first_name'];}if (empty($_REQUEST['second_name'])) {echo '

you forget type your second_name;

' ;# code...$error='2';}else{$s=$_REQUEST['second_name'];}if (empty($_REQUEST['age'])) {echo '

you forget type your age;

'; # code...$error='3';}else{$a=$_REQUEST['age'];}if (empty($error)) {echo'

everything is ok

';# code...require('connect/mysqli_connect.php');$q="insert into user (first_name,second_name,age) value('$f','$s','$a')";$r=@mysqli_query($dbc,$q);}?>

View Code

The mysqli_connect.php file under connect in the same directory.

Which is

 
View Code

There should also be a header.html file in the same directory's uplodes folder. That's all you need.

Well, the interaction is formed.

Well, good.

There are still many things to do later. Well, let's talk about it later.

References:

Larry Ullman PHP and MySQL for Dynamic Web Sites visual quickPro Guide foruth Edition

Php and mysql dynamic Website Development

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.