Heim  >  Artikel  >  Backend-Entwicklung  >  PHP-Datenzugriff: Hinzufügen, Löschen, Ändern und Überprüfen der Operations_php-Fähigkeiten

PHP-Datenzugriff: Hinzufügen, Löschen, Ändern und Überprüfen der Operations_php-Fähigkeiten

WBOY
WBOYOriginal
2016-05-16 09:00:081897Durchsuche

Eine kleine Übung zum Hinzufügen, Löschen, Ändern und Überprüfen, lass es uns üben

1. Sehen Sie sich die Nachrichtenseite an-----Hauptseite

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>查看新闻</title>
</head>
 
<body>
<h1>查看新闻</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>

 

2. Nachrichtenseite veröffentlichen -----Inhalt hinzufügen

<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><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>

 

Verarbeitung nach Übermittlung von Inhalten:

<&#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 "添加新闻失败!";
  }

3. Löschung von Inhalten

<&#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;>

4. Ändern Sie die Nachrichtenseite ---- ändern Sie den Nachrichteninhalt und senden Sie ihn zur Anzeige

<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><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>  

 

Senden Sie den geänderten Inhalt zur Bearbeitung:

<&#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 "修改数据失败!";
  }

Das Obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, dass er für alle beim Erlernen der PHP-Programmierung hilfreich sein wird.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn