Modification de la liste des news, nouvelle fonction code 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>";
    }
}
?>

Le code ci-dessus est l'opération d'ajout et de modification de la base de données. La maîtrise des instructions SQL d'ajout, de suppression, de modification et de vérification sera très utile pour les fonctions futures.


Formation continue
||
<?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>"; } } ?>
soumettreRéinitialiser le code
  • Recommandations de cours
  • Téléchargement du didacticiel