新聞列表之修改,新增功能PHP...LOGIN

新聞列表之修改,新增功能PHP代碼

<?php
 header("content-type:text/html;charset=utf-8");
 $link = mysqli_connect('localhost','root','root','news');
 mysqli_set_charset($link, "utf8");
 if (!$link) {
     die("连接失败:".mysqli_connect_error());
 }
 //接收id值
$id = isset($_GET['id'])?$_GET['id']:'';
//判断id是否有值,有值:执行查询当前需编辑的新闻内容
if($id){
    $sqlS = "select title,content,author from new where id={$id}";
    $ret = mysqli_query($link, $sqlS);
    $row = mysqli_fetch_assoc($ret);
}
if($_POST){
    $title = isset($_POST['title'])?$_POST['title']:"";
    $author = isset($_POST['author'])?$_POST['author']:"";
    $content = isset($_POST['content'])?$_POST['content']:"";
    if($id){
        $sql = "update new set title = '$title',author = '$author',content = '$content',update_at = date(curtime()) where id = '$id'";
        $tips = "更新成功";
    }else{
        $sql = "insert into new(title,content,author,create_at) values('{$title}','{$content}','{$author}',date(curtime()))";
        $tips = "添加成功";
    }
    //执行sql
    $result = mysqli_query($link, $sql);
    if($result){
        echo "<script>alert('添加成功');window.location.href='lists.php'</script>";
    }else{
        echo "<script>alert('操作失败');window.location.href='xgai.php'</script>";
    }
}
?>

上述程式碼就是資料庫的添加,修改的操作,熟練增刪改查的SQL語句,對以後做功能有很大幫助。


下一節
<?php header("content-type:text/html;charset=utf-8"); $link = mysqli_connect('localhost','root','root','news'); mysqli_set_charset($link, "utf8"); if (!$link) { die("连接失败:".mysqli_connect_error()); } //接收id值 $id = isset($_GET['id'])?$_GET['id']:''; //判断id是否有值,有值:执行查询当前需编辑的新闻内容 if($id){ $sqlS = "select title,content,author from new where id={$id}"; $ret = mysqli_query($link, $sqlS); $row = mysqli_fetch_assoc($ret); } if($_POST){ $title = isset($_POST['title'])?$_POST['title']:""; $author = isset($_POST['author'])?$_POST['author']:""; $content = isset($_POST['content'])?$_POST['content']:""; if($id){ $sql = "update new set title = '$title',author = '$author',content = '$content',update_at = date(curtime()) where id = '$id'"; $tips = "更新成功"; }else{ $sql = "insert into new(title,content,author,create_at) values('{$title}','{$content}','{$author}',date(curtime()))"; $tips = "添加成功"; } //执行sql $result = mysqli_query($link, $sql); if($result){ echo "<script>alert('添加成功');window.location.href='lists.php'</script>"; }else{ echo "<script>alert('操作失败');window.location.href='xgai.php'</script>"; } } ?>
章節課件