PHP 增刪改查

PHP 增刪改查是最最基礎的,我這段代碼適合初學者,看懂之后多敲幾遍

一 、建立數(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 ;
QQ圖片20170913102318.png

二、主要顯示頁面 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>時間</th>
            <th>修改</th>
            <th>操作</th>
        </tr>
        <?php
//        鏈接數(shù)據(jù)庫
      $db = new MYSQli("localhost","ccc","123","demo");
//        返回鏈接錯誤描述
      !mysqli_connect_error() or die("連接失敗!");
//        查詢整個表單
      $sql="select * from news";
//        執(zhí)行數(shù)據(jù)庫查詢     —>調用程序方法
      $result=$db->query($sql);
//        $arr 最為關聯(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>
//                  給腳本處理 添加動作
                  <td><a href='jihe.php?action=delate&newsid={$v[0]}'>刪除</a></td>
                  </tr>";
        }
        ?>
    </table>
<!--    點擊跳轉-->
    <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后面的動作
$act=$_GET['action'];
//echo $act;
//die;
//監(jiān)聽動作
switch($act){
//    添加
    case 'add':
//        print_r($_POST);
//        $newsid=$_POST["newsid"];
        $title=$_POST["title"];
        $author=$_POST["author"];
        $source=$_POST["source"];
        $content=$_POST["content"];
//        時間
        $time=date('y-m-d h:i:s',time());
//        鏈接數(shù)據(jù)庫
        $db = new MYSQli("localhost","ccc","123","demo");
//        返回錯誤
        !mysqli_connect_error() or die("聯(lián)系失敗!");
//        插入一條新紀錄
        $sql="insert into news values('','{$title}','{$author}','{$source}','{$content}','{$time}')";
//        執(zhí)行數(shù)據(jù)庫的查詢返回的值
        $result=$db->query($sql);
//        成功
        if($result)
        {
            echo  "<script>
            alert('添加成功');
//            跳轉頁面
            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)系失敗!");
//        更新紀錄
        $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("連接失敗!");
//        刪除紀錄
        $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");
//獲取當前id的所有
$sinfo = "select * from news where newsid='{$newsid}'";
//執(zhí)行語句
$r = $db->query($sinfo);
//這個人的所有信息 數(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">標題:<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>
<!--動態(tài)傳值,后臺處理-->
<form action="jihe.php?action=add" 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">內(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>

增刪改查,本來是分開寫的后來合并到jihe.php里面,有的東西并沒有修改,還有很多優(yōu)化的地方。適合新手閱讀。多多點贊哦

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容