Create Database Web Application Using PHP Object Oriented Programming

1. Development environment:

1. Windows 7 32-bit operating system

2. Web server: Apache2.2 32-bit (Apache-2.2.17-win32-x86-openssl-0.9.8o.msi)

3、PHP:PHP5.3 Win32(php-5.3.27-Win32-VC9-x86.zip)

4. MySql database: MySql5.1 (mysql-essential-5.1.73-win32.msi)

 

Second, the basic installation configuration:

1. Install and configure the Apache server as port 8080 (Listen8080).

       Modify the httpd.conf file

2. Configure the PHP service

       In the httpd.conf file add:

LoadModule php5_moduleD:/php-5/php5apache2_2.dll

PHPIniDir "D:/php-5"

AddType application/x-httpd-php .php .phtml

3. Enable php_mysql, php_mysqli and php_mbstring extension services in the PHP configuration

       Modify the php.ini file in the PHP folder

4. Create a web virtual folder ThinkPHP

       Modify in the Apache configuration file httpd.conf:

<IfModule dir_module>

    #Set default loading page

   DirectoryIndex index.html index.php

    #set site alias

   Alias /web "G:/fxp/PHP/phpWeb"

   <Directory G:/fxp/PHP/phpWeb>

       #Set access permissions

              Order Allow,Deny

              Allow from all

   </Directory>

</IfModule>

3. Basic project settings:

1. Create a project folder under the G:/fxp/PHP/phpWeb folder: pop;

2. Create a new index.php file in this folder with the following contents:

<?php

       echo 'php page';

?>   

3. Enter: http://localhost:8080/web/pop/ in the browser, the browser displays: php page.

Fourth, create the database and table

1. Select "MySQL"->"MySQL Server 5.1"->"MySQL Command Client" in the start menu

Open the MySQL command line operation window:

Enter the root user password to enter the MySQL command line operation mode.

2. Create database tempdb

create database tempdb;

3. Create a data table

use tempdb;

create table tb_user(id int (10)AUTO_INCREMENT primary key,

username varchar(80),userpassvarchar(80),tel varchar(20));

4. Add data to the table tb_user

insert into tb_user(username,userpass,tel)values('admin', md5('123456'), '7186121');

5. CRUD operations on the tb_user table

1. Display user information

First write a shared file conn.php for database connection, the content is as follows:

<?php

       // use MySQLi class

       $mysqli = new MySQLi('localhost','root','123456','tempdb');

       if($mysqli->error){

              die( 'Failed to connect to MySQL: '.$mysqli->error);

       }

       $mysqli->set_charset('utf8');

?>

Design the display user information page showUser.php, the content is as follows:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Browse user information</title>

</head>

<?php

       include 'conn.php'; //Include the code to create the database connection

       $sql = 'select * from tb_user';

       $result = $mysqli->query($sql); //return mysqli_result class object

       if( !$result ){

              die('Failed to get data!');

       }

?>

<body>

<div style="width:700px; margin:auto">

       <table border="1" bordercolor="#fff" bgcolor="#CCCCCC">

       <tr align="center">

              <td width="20">ID</td>

              <td width="100">Username</td>

              <td width="280">密码</td>

              <td width="100">电话号码</td>

              <td width="100">修改操作</td>

              <td width="100">删除操作</td>

       </tr>

       <?php while($record=$result->fetch_object()){//?>

       <tr>

              <td><?php echo $record->id;?></td>

              <td><?php echo $record->username;?></td>

              <td><?php echo $record->userpass;?></td>

              <td><?php echo $record->tel;?></td>

              <td><a href="modiUser.php?id=<?php echo $record->id;?>">修改</a></td>

              <td><a href="delUser.php?id=<?php echo $record->id;?>">删除</a></td>

       </tr>

       <?php } ?>

       </table>

       <p align="center"><a href="addUser.html">增加新用户</a></p>

</div>

<?php $mysqli->close(); //关闭数据库连接?>

</body>

</html>

 

2、删除用户信息

在上述显示用户信息的页面中有删除用户的超链接,带有参数id=id值,只需要在程序中执行一个'delete from tb_user where id=#'的查询就可以实现用户数据的删除。代码如下:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>浏览用户信息</title>

</head>

<?php

       include 'conn.php';         //包含创建数据库连接的代码

       $sql = 'delete from tb_user where id='.$_GET['id'];

       $result = $mysqli->query($sql);

       if( $result ){

              echo "<script> alert('删除一个用户信息'); window.location.href='showUser.php'; </script>";

       }else{

              echo "<script> alert('无用户信息'); window.location.href='showUser.php'</script>";

       }

       $mysqli->close(); //关闭数据库连接

 ?>

<body>

</body>

</html>

 

3、修改用户信息

修改用户信息,先根据从查询用户信息界面传过来的id值从数据库中获取用户信息,并显示在表单的文本框中,然后提交给modiUserSave.php处理。

modiUser.php代码如下:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>修改用户信息</title>

<?php

       include 'conn.php';

       $sql = 'select * from tb_user where id='.$_GET['id'];

       $result = $mysqli->query($sql);

       if(!$result){

              die('无此用户!');

       }

       $row = $result->fetch_row();

?>

</head>

<body>

<div style="width:350px; margin:auto">

       <form action="modiUserSave.php" method="post">

              <div style="height:40px;">

                            <input type="hidden" name="id" value="<?php echo $row[0]?>"/>

                            <input type="hidden" name="userpass" value="<?php echo $row[2]?>"/>

                     <div style="width:100px; text-align:right; float:left;">用户名:</div>

                     <div style="width:200px; text-align:left; float:left;">

                            <input type="text" name="username" value="<?php echo $row[1]?>"/>

                     </div>

              </div>

              <div style="height:40px;">

                     <div style="width:100px; text-align:right; float:left;">电话号码:</div>

                     <div style="width:200px; text-align:left; float:left;">

                            <input type="text" name="tel" value="<?php echo $row[3]?>"/>

                     </div>

              </div>

              <div style="height:40px; text-align:center;">

                     <input type="submit" value="保存数据"/>

              </div>

       </form>

</div>

<?php     $mysqli->close(); //关闭数据库连接

?>

</body>

</html>

 

modiUserSave.php代码如下:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>保存用户信息</title>

</head>

<?php

       include 'conn.php';         //包含创建数据库连接的代码

       $sql = 'update tb_user set username="'.$_POST['username'].'",tel="'.$_POST['tel'].'" where id='.$_POST['id'];

       $result = $mysqli->query($sql);

       if( $result ){

              echo "<script> alert('保存修改用户信息成功'); window.location.href='showUser.php'; </script>";

       }else{

              echo "<script> alert('保存修改用户信息失败'); window.location.href='showUser.php'; </script>";

       }

       $mysqli->close(); //关闭数据库连接

 ?>

<body>

</body>

</html>

 

4、添加新用户

添加新用户由addUser.html页面和addUserSave.php程序组成。addUser.html页面代码如下:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>添加用户信息</title>

</head>

<body>

<div style="width:350px; margin:auto">

       <form action="addUserSave.php" method="post">

              <div style="height:40px;">

                     <div style="width:100px; text-align:right; float:left;">用户名:</div>

                     <div style="width:200px; text-align:left; float:left;">

                            <input type="text" name="username" value=""/>

                     </div>

              </div>

              <div style="height:40px;">

                     <div style="width:100px; text-align:right; float:left;">密&nbsp;&nbsp;码:</div>

                     <div style="width:200px; text-align:left; float:left;">

                            <input type="password" name="userpass" value=""/>

                     </div>

              </div>

              <div style="height:40px;">

                     <div style="width:100px; text-align:right; float:left;">电话号码:</div>

                     <div style="width:200px; text-align:left; float:left;">

                            <input type="text" name="tel" value=""/>

                     </div>

              </div>

              <div style="height:40px; text-align:center;">

                     <input type="submit" value="保存新增用户信息"/>

              </div>

       </form>

</div>

</body>

</html>

addUserSave.php程序从addUser.html页面获取提交的数据,然后存入数据库表中。addUserSave.php代码如下:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>保存新增用户信息</title>

</head>

<?php

       include 'conn.php';         //包含创建数据库连接的代码

       $sql = 'insert into tb_user(username,userpass,tel) values("'.$_POST['username']

                     .'","'.md5($_POST['userpass']).'","'.$_POST['tel'].'")';

       $result = $mysqli->query($sql);

       if( $result ){

              echo "<script> alert('保存添加用户信息成功'); window.location.href ='showUser.php'; </script>";

       }else{

              echo "<script> alert('保存添加用户信息失败'); window.location.href ='showUser.php'; </script>";

       }

       $mysqli->close(); //关闭数据库连接

 ?>

<body>

</body>

</html>

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325740057&siteId=291194637