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

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

前のセクションでは、データベースでのクエリとコードの表示について説明しました。次に、変更関数の説明を続けます。まず、前のレッスンの完全なソース コードを確認してください

<?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");//设置数据库的字符集

次にフォーム情報を取得します

 <?php 
    $id = $_GET['id'];
    $title = $_POST['title'];
    $content = $_POST['content'];
    $messtime = time();

注: ここで ID も取得する必要があります。前のレッスンでは、ID を取得してデータベースにクエリを実行しました。ここでは、その情報を変更するなどの条件が必要です。次に、次のように記述します。変更ステートメント

$sql = "update new set title=' $title',content='$content',messtime='$messtime' where id='$id'";

$res = mysql_query($sql );

ITを書いた後に修正されたステートメントを判断しますif($ res){

echo "&lt; script&gt; alert( 'Modification custed'); location.href = 'newlist.php';&lt;( '修正が失敗しました');history.go(-1);</script>";

}

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

<?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'];
	$title = $_POST['title'];
	$content = $_POST['content'];
	$messtime = time();

	$sql = "update new set title='$title',content='$content',messtime='$messtime' where id='$id'";
	$res = mysql_query($sql);
	if($res){
		echo "<script>alert('修改成功');location.href='newlist.php';</script>";
	}else{
		echo "<script>alert('修改失败');history.go(-1);</script>";
	}
?>

このようにして、単純な関数の修正が完了しました
次のセクション
コースウェア