2012-08-02 3 views
0

아래의 코드는 내가 작성한 프로그램이다. 문제는, 제출, 답장 또는 삭제 버튼을 눌러도 작동하지 않는 것입니다.나는 PHP와 MySQL을 사용하여 포럼을 만드는 데 어려움을 겪고있다.

아래에 작성한 코드의 문제점을 해결할 수 있습니까? 정말 고마워요.

<?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> 
+0

이전에 출력이있는 경우'header ('Location : ...')'가 작동하지 않습니다. – romainberger

답변

0

어떻게하면 좋을까요?

print "<input type='submit' name='action' value='write'>\n"; 
print "<hr>\n"; 
print "記事番号:<input type='text' name='delno'>\n"; 
print "削除キー: <input type='password' name='delkey'>\n"; 
print "<input type='submit' name='action' value='delete'>\n"; 
print "<input type = 'hidden' name = 'action' value = 'delete'>\n"; (remove this line) 
+0

조언 해 주셔서 감사합니다. 그러나 언급 한 행을 제거하더라도 작동하지 않습니다. – kenken

+0

나는 필드의 이름이 action (둘 다 submits)이고 또한 이라는 3 개의 라인이 여전히 있음을 보았다. 서로 다른 세 줄의 이름을 바꿀 수 있습니까 (이름 그대로)? 또한 페이지를로드하거나 제출 버튼 중 하나를 클릭하면 자바 스크립트 오류가 발생합니까? – Thomas

+0

도와 주셔서 감사합니다. 이름을 변경하고 다시 시도했지만 여전히 작동하지 않습니다. 이 프로그램은 PHP와 MySQL로 작성되었으므로 자바 스크립트 오류가 발생하지 않습니다. – kenken

관련 문제