下面的代码是我编写的程序。问题是,即使我按下提交、回复或删除,按钮都不起作用。
你能帮我看看我在下面写的代码有什么问题吗?太感谢了。
<?php
/*共通処理*/
//データベースへの接続
try{
$GLOBALS['db'] = new PDO("mysql:host=localhost; dbname=bbs", "root", "test");
} catch (PDOException $e) {
echo 'connection failed: '.$e->getMessage();
}
//action取得
$action = (isset($_GET['action']) ? $_GET['action'] : $_POST['action']);
//action振り分け
switch($action)
{
//書き込み処理
case "regist":
proc_regist();
break;
//削除処理
case "delete":
proc_delete();
break;
//返信処理
case "res":
proc_res();
break;
//表示処理
default:
proc_default();
break;
}
// 終了処理
/* ここで処理は終了 あとは個別の関数へ */
// 基本の掲示板表示処理
function proc_default()
{
$db = $GLOBALS['db'];
$page_max = 15;
$offset = ( isset($_GET['offset']) ? $_GET['offset'] : 0 );
$limit = $page_max +1;
$stmd = $db->query("select * from message order by no desc limit $limit offset $offset");
$rows = $stmd->fetchAll();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "ja">
<head>
<meta http-equiv = "Content-Type" content="text/html; charset = UTF-8">
<title>
</title>
</head>
<body>
<form method = "POST" action = "bbs_new.php">
<input type = "hidden" name = "action" value= "regist">
お名前:<input type="text" name="name"><br>
メール:<input type="text" name="mail"><br>
題 名:<input type="text" name="title"><br>
削除キー:<input type="password" name="delkey" value="<?php print $delkey ?>"><br>
<textarea name="contents" cols="60" rows="5"></textarea><br>
<?php
print "<input type='submit' name='write' value='送信'>\n";
print "<hr>\n";
print "記事番号:<input type='text' name='delno'>\n";
print "削除キー: <input type='password' name='delkey'>\n";
print "<input type='submit' name='delete' value='記事削除'>\n";
print "<input type = 'hidden' name = 'action' value = 'delete'>\n";
?>
</form>
<?php
//ここからデータ表示処理
$sql = 'select * from message order by no desc';
foreach ($db->query($sql) as $row) {
if ($_GET['resno']) {
if ($row['resno'] != $_GET['resno']) continue;
}
if ($_POST['resno']) $resno = $_POST['resno'];
else $resno = $no;
if ($row['no'] != $row['resno']) $res = true; else $res = false;
if ($res) print "<blockquote>";
else print "<hr>";
print "<p>No.".$row['no']." ";
print "<b>{$row['title']}</b> 投稿者:";
if ($row['mail']) print "<a href='mailto:{$row['mail']}'>";
print $row['name'];
if ($row['mail']) print "</a>";
$row['time'] = date("Y/m/d H:i:s");
print " 投稿時間:{$row['time']}";
if (!$res && !$_GET['resno']) {
print " <a href='bbs_new.php?resno={$row['no']}'>返信</a>";
}
print "<br><br>{$row['contents']}</p>";
if ($res) print "</blockquote>";
print "\n";
}
//改ページ
if(count($rows)>$page_max){
$next = $offset+$page_max;
print "[<a href='bbs_new.php?offset=$next'>前のページ</a>]";
}
if ($offset>0){
$prev = $offset-$page_max;
print "[<a href='bbs_new.php?offset=$prev'>次のページ</a>]";
}
//rowに1行ずつ取得したデータが入る
//返信データの有無
$sql2 = 'select * from message order by no desc';
foreach ($db->query($sql2) as $row2) {
//入力フォームの書き出し
if ($_GET['resno']) {
print "<input type='submit' name='write' value='No.{$_GET['resno']} に返信'>\n";
print "<input type='hidden' name='resno' value='{$_GET['resno']}'>\n";
} else {
print "<input type='submit' name='write' value='送信'>\n";
print "<hr>\n";
print "記事番号:<input type='text' name='delno'>\n";
print " 削除キー: <input type='password' name='delkey2'>\n";
print " <input type='submit' name='delete' value='記事削除'>\n";
}
}
}
//header("Location: bbs_new.php");
//データベースへ書き込みを行う処理
function proc_regist(){
// グローバル変数から取り出す
$db = $GLOBALS['db'];
//記事番号
$sql = 'select no from message order by no desc';
$maxno = 0;
$no = $maxno + 1;
$_POST['no'] = $no;
if(!$_POST['resno']){
$_POST['resno'] = $_POST['no'];
}
//データベースへインサート
$sql = 'insert into message values(?,?,?,?,?,?,?,?)';
$st = $db->prepare($sql);
$result = $st->execute(array($_POST['no'], $_POST['resno'],$_POST['name'], $_POST['mail'],$_POST['title'], $_POST['contents'],$_POST['delkey'], $_POST['time']));
}
header("Location: bbs_new.php");
//データベースから削除を行う処理
function proc_delete(){
$db = $GLOBALS['db'];
$sql = 'delete from message where no = ? and delkey = ? ';
$sth = $db->prepare($sql);
$ret = $sth->execute(array($_POST['no'],$_POST['delkey']));
header("Location: bbs_new.php");
}
?>
</body>
</html>