WebプログラミングU PHP

 目次へ 

2009/9/1 久米

 

総まとめ 重要ポイント

 

PHPの基礎

フォーム投稿

ファイルf.htm   ファイルm.php
<form method="post" action="m.php">
メッセージ<input type="text" name="message">
<input type="submit" name="sub" value="投稿">
</form>
<?php
$m= "変数messageの値は ".$_POST['message'];
print $m;
?>

データベース連携

SELECT文
<?php
$con=mysql_connect("localhost", "root", "root");
$sel=mysql_select_db("kume", $con);
$sql="SELECT * FROM ranking ORDER BY point DESC";
$rst=mysql_query($sql, $con);

$m="";
while($row=mysql_fetch_array($rst)){
    $m.=$row["id"]." ";
    $m.=$row["title"]." ";
    $m.=$row["artist"]." ";
    $m.=$row["point"]."<br>";
}
mysql_free_result($rst);
$cls=mysql_close($con);
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>select文</title>
</head>
<body>
<?php print $m; ?>
</body>
</html>
UPDATEや
INSERTや
DELETE文
<?php
$con=mysql_connect("localhost", "root", "root");
$sel=mysql_select_db("kume", $con);
$sql="UPDATE ranking SET point=1980 WHERE title='COLORS'";
$rst=mysql_query($sql, $con);

$m="";
if($rst) $m="成功しました。";
else $m="失敗しました。";
$cls=mysql_close($con); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <title>select文</title> </head> <body> <?php print $m; ?> </body> </html>

セッション管理クッキー

セッション管理の宣言 や クッキーの設定は <html>タグの前に行なう。