这个系列的教程由表单开始写,因为表单可以把数据库和web 之间的交互表现得很明显。提交表单 ,数据库记录注册信息。
本教程属于基础教程。大神请略过。
对于php和mysql之间的稳固性很受程序员的喜爱。虽说最近出现了hack语言准备替代php语言,但是这个语言本身也是基于php的,后面我们可以对hack语言进行一定得学习。
好像博客园有好多大牛,不知道会不会被喷,好怕 .......。当然,我是借着自己学习的这股劲,把学习的过程也给记录下来。教程涉及 html 表单 简单的php 和mysql .先用最简单的表单,意思一下之间的交互就行了。
记得当我们什么都不懂的时候,代码也不清楚的时候,我们在想着奇幻的代码世界,感觉无比的奇妙,当我们步入代码世界的时候,我们又感到茫然了。代码是什么。此处从html说起。当然,要是html都不懂, 也不会到博客园来的。随便说一下 就是浏览器中右击,查看源代码的那个代码就是html了。但是它只是网页的结构了,要想形成一个漂亮的网页当然要很多东西了 ,其他的有机会再说,这次说网页之间的交互了。也就是动态网站的开发。
要是表单都不知道的话,搜索一下。就是那个注册的时候,那些框框了。
下面是表单代码。
就是这一串代码,保存好后,网页打开,只有框子在 ,点击也没有什么用,当然这就是前端了。 我们今天说的后端。你需要的就是把那个输入的部分 传送到所谓的数据库中去保存好,这样方便网站管理。到底怎么样传过去的呢?这里就要说php 和mysql 了。注意: 运行php 和mysql 的时候,是要有环境的。要有它们运行的基础。它们的环境,要不然怎么生存呢。对吧? 然后有个很有名的一件套件包。包括了这些环境。下一步 下一步 安装就行了。
地址:http://apmserv.s135.com/ 尊重原来的地址。
貌似windows 8上装不了。哎。。。
安装好,先熟悉一下吧。恩 ~~
php 就是在 代码区 /code ?>在中间写php代码,文件时.php格式,里面可以加入 html语言 。恩,把表单放进去吧。
放进去了 访问它还是原来一样一样的。然后让它变成动态的吧。加入几个变量。
<p>please type your first name:<input type="text" name="first_name" value="<? if(isset($_REQUEST['first_name'])) echo $_REQUEST['first_name'];?>"></p><p>please type your second name:<input type="text" name="second_name" value="<? if(isset($_REQUEST['first_name'])) echo $_REQUEST['second_name'];?>"></p><p>please type your age:<input type="text" name="age" value="<? if(isset($_REQUEST['age'])) echo $_REQUEST['age'];?>"></p><p><input type="submit" name="submit" value="register"></p>
恩,很熟悉的表单。然后你要把输入的存在了那个变量里面。 这里就要说php 数组了,php数组中就是可以用字符串数组的形式。就是array['name'] 然后name 可以是一些你定义的了。这里对于表单很特别了,这个REQUEST 属于超全局变量,好像很酷的样子,就是不用定义,它也一直在,就是表单的名字放在里面,输入的
内容就保存到这个数组里面了,当然 其实也可以用 $_POST['name'] 了 这个Post 呢 属于REQUEST的子集了。反正这样表示都是一样一样的。
然后你的数据都保存到这个数组中了,然后提取它就很方便了,这个中间用了判断这个里面是不是填了数据,然后返回,这个isset() 就是判断是不是填了数据,然后就和html 的表单一样一样的。你还要判断这个数据是不是填了,总要有个判断吧。
就有了下面这段。
$error=array();if (empty($_REQUEST['first_name'])) { echo '<p>you forget type your first_name;</p><p>' ;# code... $error='1';}else{ $f=$_REQUEST['first_name'];}if (empty($_REQUEST['second_name'])) { echo '</p><p>you forget type your second_name;</p><p>' ;# code... $error='2';}else{ $s=$_REQUEST['second_name'];}if (empty($_REQUEST['age'])) { echo '</p><p>you forget type your age;</p><p>'; # code... $error='3';}else{ $a=$_REQUEST['age'];}</p>
这个empty()就是判断 是不是空的表示 ,echo 就是php 的输出了。可以把html同样输出出来。定义一个error数组
就是判断有没有错误,如果都填了的话。就ok了.
然后又把那些变量保存到另一些变量里面去。这样看起来简单点。
恩,然后,发现没有错误。
然后返回这一段,恩,这一段就是最主要的部分了,就是靠它连接我们的数据库了。
if (empty($error)) { echo'<p> everything is ok</p><p>'; require('connect/mysqli_connect.php'); $q="insert into user (first_name,second_name,age) value('$f','$s','$a')"; $r=@mysqli_query($dbc,$q);}</p>
那个require( )就是需要连接数据库的文件。数据库的地址啊,表名啊,用户名啊,密码啊 ,靠这些联系数据库。
这个文件就换一个地方去了.直接调用它过来 连接就行了。就是require了。
这个就是 mysqli_connect.php了.
$dbc=@mysqli_connect(localhost,root,123,test);?>
尽量简单一点,哈哈 好短的文件,这个文件就保存在index.php 的同一目录下的connect 文件夹里面. 里面有一个函数 mysqli_connect() 这个函数就是连接数据库用的呀。
mysqli_connect(hostname,username,password,databasename)
数据的地址名 一般都是localhost 了,当然也不一定。看你的数据库在哪。然后就是 账户,密码,数据库名。
然后它返回的保存在一个变量里面。ok了 调用,然后就连上数据库了
下面 又定义一个$q,这就又说到了 数据库的知识了,在数据库中,你要创建数据库呀 ,表名呀,列 呀,然后一一匹配的把刚才那些变量传送过去。
恩,这就说说数据库了。恩~~ 啊~~。
数据库管理现在 好多都是 phpadmin之类的东东了,好像很方便的样子。在那个东东里面 ,有个sql 命令的地方,你要输入点小命令,创建刚才说的那些东东。
先来创建一个数据库吧。 输入的是
create database test;
这就创造了一个数据库。 test是数据库的名字了。 很简单样子。
下面就是创建表名了。还是用create
create table user(first_name varchar(20) not null,second_name varchar(20) not null,age varchar(20) not null);
写程序一定要注意一些分号呀,冒号呀,逗号啊 等等。注意这些在哪个地方,怎么用,中文和英文版的分号的区别呀 ,要不然就出错了。
这个就是创造表名和列名了。
这个东东 varchar(20) 你就要查 mysql数据类型了。就是定义它是个什么类型的数据了 前面就是列名了。
这样就创造了 一个user表名 三个列名 first_name second_name age ;
好了数据传送过去
$q="insert into user (first_name,second_name,age) value('$f','$s','$a')";
就是这个东东了 ,insert into user ( , , , ,) value( , , , , ,);
插入 恩 插入 这些数据进入。一一对应 前面是列名 后面就是它的值了 。 后面不就是刚才保存的变量吗 恩。输入的东东就进去了。
$r=@mysqli_query($dbc,$q);
这个函数也很重要。 mysqli_query( ) 前面是那个连接 ,后面参数是查询本身。
这样就连接上了 。咚咚咚 ~~~ 运行起来了。。
你输入东西到表单上去。然后 点击提交,然后 刷新数据库 是不是发现列名多了东西
全部的代码就是下面主要的index.php

<?include ('includes/header.html');echo '<p>this is a test';$error=array();if (empty($_REQUEST['first_name'])) { echo '<p>you forget type your first_name;</p><p>' ;# code... $error='1';}else{ $f=$_REQUEST['first_name'];}if (empty($_REQUEST['second_name'])) { echo '</p><p>you forget type your second_name;</p><p>' ;# code... $error='2';}else{ $s=$_REQUEST['second_name'];}if (empty($_REQUEST['age'])) { echo '</p><p>you forget type your age;</p><p>'; # code... $error='3';}else{ $a=$_REQUEST['age'];}if (empty($error)) { echo'</p><p> everything is ok</p><p>';# code... require('connect/mysqli_connect.php'); $q="insert into user (first_name,second_name,age) value('$f','$s','$a')"; $r=@mysqli_query($dbc,$q);}?></p>View Code
同目录下connect 下的mysqli_connect.php 文件。
里面就是

$dbc=@mysqli_connect(localhost,root,123,test);?>View Code
应该还有一个在同目录下的includes文件夹下的 header.html 文件。这个就随便了。
恩,这样交互 就形成了。
恩 ,不错.
后面接着还有好多事情要干呢。恩 ,后面再说吧。
参考资料:
larry Ullman 《PHP and MySQL for Dynamic Web Sites visual quickPro Guide foruth Edition》
《php 与mysql动态网站开发》

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor