PHPで開発したニュース管理シ...ログイン

PHPで開発したニュース管理システムの修正機能の実装(前編)

前のセクションで述べたように、2 つの接続を変更および削除するときに、id

<a href="modifynew.php?id=" を出力するステートメントを作成したことに気づきましたか。 <? php echo $row['id'];?>">変更</a>

<a href="delnew.php?id=<?php echo $row['id'] ;? >">削除</a>

変更するには、ID を取得し、その ID に基づいてデータからクエリを実行し、ID の他のフィールドの内容を変更する必要があります。次を見てみましょう。変更されたフローチャート

md.png

[変更]をクリックすると、ファイルmodifynew.phpにIDが転送されます

このページは変更されたページであり、その効果は次のとおりです:

104.png

このページでは、渡された ID に基づいて続行する必要があります。データベースにクエリを実行してから、タイトルのコンテンツを表示します

まずデータベースに接続します:

header("Content-type: text/html; charset=utf-8");/ /エンコーディングを設定します
$con =@mysql_connect("localhost ","root","root") or die("データベース接続に失敗しました");
mysql_select_db('news') or die("指定されたデータベースを開けません");
mysql_query("set names utf8");/ /データベースの文字セットを設定します

そして ID を取得します

フォーム上の ID get メソッドを使用して

$id=$ を取得します_GET['id'];

id に基づいてデータベースにクエリを実行します

$sql ="select * from new where id=$id";
$res = mysql_query($sql);
$row = mysql_fetch_array( $res);

情報をクエリした後、ページに情報を表示する必要があります

HTMLページのコードは次のとおりです:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
    *{margin:0px;padding:0px;}
    body{background:#ccc;}
    .add{width:450px;height:280px;background:#eee;float:left;}
    .cont{width:500px;height:350px;margin-top:5px;margin-left:5px;}
    form{margin-left:10px;padding-top:30px;}
    .sub{width:100px;height:40px;border:1px solid #ccc;}
    .sub:hover{background:#f90}
    </style>
</head>
<body>
    <div class="add">
        <div class="cont">
            <form method="post" action="modify.php?id=<?php echo $id;?>">
                标题:<input type="text" name="title" value="<?php echo $row['title']?>"></br></br>
                内容:<textarea cols="50" rows="5" name="content"><?php echo $row['content']?></textarea></br></br>
                <input type="submit" value="修改" class="sub">
            </form>
        </div>
    </div>
</body>
</html>

このようにして、データベースからクエリした情報が表示されます


完全なソースコードは次のとおりです:

<?php
    header("Content-type: text/html; charset=utf-8");//设置编码
    $con =@mysql_connect("localhost","root","root") or die("数据库连接失败");
    mysql_select_db('news') or die("指定的数据库不能打开");
    mysql_query("set names utf8");//设置数据库的字符集

    $id=$_GET['id'];
    $sql="select * from new where id=$id";
    $res = mysql_query($sql);
    $row = mysql_fetch_array($res);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
    *{margin:0px;padding:0px;}
    body{background:#ccc;}
    .add{width:450px;height:280px;background:#eee;float:left;}
    .cont{width:500px;height:350px;margin-top:5px;margin-left:5px;}
    form{margin-left:10px;padding-top:30px;}
    .sub{width:100px;height:40px;border:1px solid #ccc;}
    .sub:hover{background:#f90}
    </style>
</head>
<body>
    <div class="add">
        <div class="cont">
            <form method="post" action="modify.php?id=<?php echo $id;?>">
                标题:<input type="text" name="title" value="<?php echo $row['title']?>"></br></br>
                内容:<textarea cols="50" rows="5" name="content"><?php echo $row['content']?></textarea></br></br>
                <input type="submit" value="修改" class="sub">
            </form>
        </div>
    </div>
</body>
</html>


次のセクション
<?php header("Content-type: text/html; charset=utf-8");//设置编码 $con =@mysql_connect("localhost","root","root") or die("数据库连接失败"); mysql_select_db('news') or die("指定的数据库不能打开"); mysql_query("set names utf8");//设置数据库的字符集 $id=$_GET['id']; $sql="select * from new where id=$id"; $res = mysql_query($sql); $row = mysql_fetch_array($res); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> *{margin:0px;padding:0px;} body{background:#ccc;} .add{width:450px;height:280px;background:#eee;float:left;} .cont{width:500px;height:350px;margin-top:5px;margin-left:5px;} form{margin-left:10px;padding-top:30px;} .sub{width:100px;height:40px;border:1px solid #ccc;} .sub:hover{background:#f90} </style> </head> <body> <div class="add"> <div class="cont"> <form method="post" action="modify.php?id=<?php echo $id;?>"> 标题:<input type="text" name="title" value="<?php echo $row['title']?>"></br></br> 内容:<textarea cols="50" rows="5" name="content"><?php echo $row['content']?></textarea></br></br> <input type="submit" value="修改" class="sub"> </form> </div> </div> </body> </html>
コースウェア