Table of Contents
查看新闻
发布新闻
修改新闻
Home Backend Development PHP Tutorial php数据访问之增删改查操作_PHP

php数据访问之增删改查操作_PHP

May 27, 2016 am 10:34 AM
php Add, delete, modify and check data access

增删改查操作小练习,大家练练手吧

一、查看新闻页面-----主页面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

<html>

<head>

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

<title>查看新闻</title>

</head>

  

<body>

<h1 id="查看新闻">查看新闻</h1>

<table width="100%" border="1" cellpadding="0" cellspacing="0">

<tr>

  <td>id</td>

  <td>title</td>

  <td>author</td>

  <td>source</td>

   <td>content</td>

  <td>date</td>

  <td>update</td>

  <td>delete</td>

</tr>

<&#63;php

$db=new MySQLi("localhost","root","","mydb");

!mysqli_connect_error() or die("连接失败!");

$sql="select * from news";

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

$arr=$result->fetch_all();

foreach ($arr as $v)

{

  echo "<tr>

  <td>{$v[0]}</td>

  <td>{$v[1]}</td>

  <td>{$v[2]}</td>

  <td>{$v[3]}</td>

  <td>{$v[4]}</td>

  <td>{$v[5]}</td>

  <td><a href='Update.php&#63;newsid={$v[0]}'>update</a></td>

  <td><a href='Delete.php&#63;newsid={$v[0]}'>delete</a></td>

    

  </tr>";

  }

  

&#63;>

</table>

<br>

<br />

<div class="xw"><a href="xinwen.php">发布新闻</a></div>

  

</body>

</html>

Copy after login

  

二、发布新闻页面-----添加内容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

<html>

<head>

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

<title>发布新闻</title>

<style>

.xw

{

    

  margin-top:10px;

  margin-left:400px;

  border:thick;

  }

.a

{

  float:left;

    

  }

  

</style>

  

</head>

  

<body>

<h1 id="center-发布新闻-center"><center>发布新闻</center></h1>

  

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

<div class="xw">标题:<input type="text" name="title" style="width:400px"></div>

<div class="xw">作者:<input type="text" name="author"></div>

<div class="xw">来源:<input type="text" name="source"></div>

<div class="xw">内容:

<textarea rows="10" cols="80" name="content"></textarea></div>

  

<div class="a"><input type="submit" value="提交" style="margin-left:600px;"></div>

<div class="a"><a href="ChaKan.php"><input type="button" value="查看" style="margin-left:6px;"></a></div>

  

</form>

  

  

</body>

</html>

Copy after login

  

提交内容后的处理:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<&#63;php

$newsid=$_POST["newsid"];

$title=$_POST["title"];

$author=$_POST["author"];

$source=$_POST["source"];

$content=$_POST["content"];

$time=date("Y-m-d",time());

  

$db=new MySQLi("localhost","root","","mydb");

!mysqli_connect_error() or die("联系失败!");

$sql="insert into news values('{$newsid}','{$title}','{$author}','{$source}','{$content}','{$time}')";

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

if($result)

{

  header ("location:xinwen.php");

  }

else

{

  echo "添加新闻失败!";

  }

Copy after login

三、删除内容处理

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<&#63;php

$newsid=$_GET["newsid"];

$db=new MySQLi("localhost","root","","mydb");

!mysqli_connect_error() or die("连接失败!");

$sql="delete from news where newsid='{$newsid}'";

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

if($result)

{

  header ("location:ChaKan.php");

  }

else

{

  echo "删除数据失败";

  }

&#63;>

Copy after login
  

四、修改新闻页面----修改新闻内容后提交查看

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

<html>

<head>

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

<title>修改新闻</title>

<style>

.xw

{

    

  margin-top:10px;

  margin-left:400px;

  border:thick;

  }

    

.a

{

  float:left;

    

  }

  

</style>

</head>

  

<body>

<h1 id="center-修改新闻-center"><center>修改新闻</center></h1>

<&#63;php

$newsid = $_GET["newsid"];

$db = new MySQLi("localhost","root","","mydb");

$sinfo = "select * from news where newsid='{$newsid}'";

$r = $db->query($sinfo);

$arr = $r->fetch_row(); //这个人的所有信息

&#63;>

  

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

<div class="xw"><input type="hidden" name="newsid" value="<&#63;php echo $arr[0] &#63;>"></div>

<div class="xw">标题:<input type="text" name="title" style="width:400px" value="<&#63;php echo $arr[1] &#63;>"></div>

<div class="xw">作者:<input type="text" name="author" value="<&#63;php echo $arr[2] &#63;>"></div>

<div class="xw">来源:<input type="text" name="source" value="<&#63;php echo $arr[3] &#63;>"></div>

<div class="xw">内容:

<textarea rows="10" cols="80" name="content"><&#63;php echo $arr[4] &#63;></textarea></div>

  

<div class="a"><input type="submit" value="修改" style="margin-left:600px;"></div>

<div class="a"><a href="ChaKan.php"><input type="button" value="查看" style="margin-left:6px;"></a></div>

  

</form>

  

</body>

</html>  

Copy after login

  

提交修改内容后进行处理:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<&#63;php

$newsid=$_POST["newsid"];

$title=$_POST["title"];

$author=$_POST["author"];

$source=$_POST["source"];

$content=$_POST["content"];

$time=date("Y-m-d",time());

 

$db=new MySQLi("localhost","root","","mydb");

!mysqli_connect_error() or die("联系失败!");

$sql="update news set title='{$title}',author='{$author}',source='{$source}',content='{$content}',time='{$time}' where newsid='{$newsid}'";

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

if($result)

{

  header ("location:Update.php");

  }

else

{

  echo "修改数据失败!";

  }

Copy after login

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Services CakePHP Services Sep 10, 2024 pm 05:26 PM

This chapter deals with the information about the authentication process available in CakePHP.

See all articles