PHP 增刪改查是最最基礎(chǔ)的辅斟,我這段代碼適合初學(xué)者靖诗,看懂之后多敲幾遍
一 、建立數(shù)據(jù)庫
CREATE TABLE IF NOT EXISTS `news` (
`newsid` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`Author` varchar(255) DEFAULT NULL,
`source` varchar(255) DEFAULT NULL,
`content` varchar(255) DEFAULT NULL,
`time` datetime DEFAULT NULL,
PRIMARY KEY (`newsid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=37 ;
二沪斟、主要顯示頁面 index.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主頁面</title>
</head>
<style>
*{
margin: 0;
padding:0;
}
table th,td{
padding: 0 20px;
}
</style>
<body>
<h1>查看新聞</h1>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th>id</th>
<th>主題</th>
<th>作者</th>
<th>來源</th>
<th>內(nèi)容</th>
<th>時(shí)間</th>
<th>修改</th>
<th>操作</th>
</tr>
<?php
// 鏈接數(shù)據(jù)庫
$db = new MYSQli("localhost","ccc","123","demo");
// 返回鏈接錯(cuò)誤描述
!mysqli_connect_error() or die("連接失敼愠健!");
// 查詢整個(gè)表單
$sql="select * from news";
// 執(zhí)行數(shù)據(jù)庫查詢 —>調(diào)用程序方法
$result=$db->query($sql);
// $arr 最為關(guān)聯(lián)數(shù)組
$arr=$result->fetch_all();
// 循環(huán)遍歷
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?newsid={$v[0]}'>修改</a></td>
// 給腳本處理 添加動(dòng)作
<td><a href='jihe.php?action=delate&newsid={$v[0]}'>刪除</a></td>
</tr>";
}
?>
</table>
<!-- 點(diǎn)擊跳轉(zhuǎn)-->
<input type="submit" value="發(fā)布新聞" onclick='window.location.href="xinwen.php"'>
</body>
</html>
三主之、腳本助理 jihe.php
<?php
//編碼格式
header("Content-Type: text/html;charset=utf-8");
//處理傳至 url后面的動(dòng)作
$act=$_GET['action'];
//echo $act;
//die;
//監(jiān)聽動(dòng)作
switch($act){
// 添加
case 'add':
// print_r($_POST);
// $newsid=$_POST["newsid"];
$title=$_POST["title"];
$author=$_POST["author"];
$source=$_POST["source"];
$content=$_POST["content"];
// 時(shí)間
$time=date('y-m-d h:i:s',time());
// 鏈接數(shù)據(jù)庫
$db = new MYSQli("localhost","ccc","123","demo");
// 返回錯(cuò)誤
!mysqli_connect_error() or die("聯(lián)系失敗!");
// 插入一條新紀(jì)錄
$sql="insert into news values('','{$title}','{$author}','{$source}','{$content}','{$time}')";
// 執(zhí)行數(shù)據(jù)庫的查詢返回的值
$result=$db->query($sql);
// 成功
if($result)
{
echo "<script>
alert('添加成功');
// 跳轉(zhuǎn)頁面
window.location.href='index.php';
</script>";
}
// 失敗
else
{
echo "<script>
alert('添加失敗');
// 回退并刷新頁面
history.go(-1);
</script>";
}
break;
// 修改
case 'update';
$newsid=$_POST["newsid"];
$title=$_POST["title"];
$author=$_POST["author"];
$source=$_POST["source"];
$content=$_POST["content"];
$time=date('y-m-d h:i:s',time());
$db = new MYSQli("localhost","ccc","123","demo");
!mysqli_connect_error() or die("聯(lián)系失敗!");
// 更新紀(jì)錄
$sql="update news set newsid='{$newsid}',title='{$title}',author='{$author}',source='{$source}',content='{$content}',time='{$time}' where newsid='{$newsid}'";
$result=$db->query($sql);
if($result)
{
echo "<script>
alert('修改成功');
window.location.href='Update.php?newsid={$newsid}';
</script>";
}
else
{
echo "<script>
alert('修改失敗');
</script>";
}
break;
// 刪除
// case 'delate';
default:
$newsid=$_GET["newsid"];
$db=new MySQLi("localhost","ccc","123","demo");
!mysqli_connect_error() or die("連接失敗!");
// 刪除紀(jì)錄
$sql="delete from news where newsid='{$newsid}'";
$result=$db->query($sql);
if($result)
{
echo "<script>alert('刪除成功');location.href='index.php'</script>";
}
else
{
echo "刪除數(shù)據(jù)失敗";
}
}
四择吊、 修改主要 Update.php
<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>
<?php
$newsid=$_GET["newsid"];
//print_r($newsid);
$db = new MySQLi("localhost","ccc","123","demo");
//獲取當(dāng)前id的所有
$sinfo = "select * from news where newsid='{$newsid}'";
//執(zhí)行語句
$r = $db->query($sinfo);
//這個(gè)人的所有信息 數(shù)組
$arr = $r->fetch_row();
//echo $arr[0];
?>
<form action="jihe.php?action=update" method="post">
<div class="xw"><input type="hidden" name="newsid" value="<?php echo $arr[0] ?>"></div>
<div class="xw">標(biāo)題:<input type="text" name="title" style="width:400px" value="<?php echo $arr[1] ?>"></div>
<div class="xw">作者:<input type="text" name="author" value="<?php echo $arr[2] ?>"></div>
<div class="xw">來源:<input type="text" name="source" value="<?php echo $arr[3] ?>"></div>
<div class="xw">內(nèi)容:
<textarea rows="10" cols="80" name="content"><?php echo $arr[4] ?></textarea></div>
<div class="a"><input type="submit" value="修改" style="margin-left:600px;"></div>
<div class="a"><a href="index.php"><input type="button" value="查看" style="margin-left:6px;"></a></div>
</form>
</body>
</html>
五、 發(fā)布新聞 xinwen.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>發(fā)布新聞</title>
<style>
.xw
{
margin-top:10px;
margin-left:400px;
border:thick;
}
.a
{
float:left;
}
</style>
</head>
<body>
<h1><center>發(fā)布新聞</center></h1>
<!--動(dòng)態(tài)傳值槽奕,后臺(tái)處理-->
<form action="jihe.php?action=add" method="post">
<div class="xw">標(biāo)題:<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">內(nèi)容:
<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="index.php"><input type="button" value="查看" style="margin-left:6px;"></a></div>
</form>
</body>
</html>
增刪改查几睛,本來是分開寫的后來合并到j(luò)ihe.php里面,有的東西并沒有修改粤攒,還有很多優(yōu)化的地方所森。適合新手閱讀。多多點(diǎn)贊哦