2015-02-17 64 views
0

我正在嘗試使用PHP,HTML和MySQL製作分頁博客。我編寫了代碼,但由於某種原因,網頁顯示爲空白。我的代碼有什麼問題? Chrome的控制檯返回500內部服務器錯誤。我的博客分頁不起作用

<div id="article"> 

<?php 
    include 'php/mysql_connect.php'; 
    if(empty($_GET)){ 
    $current_id = SELECT max(id) FROM posts; 
    } 
    else{ 
    $current_id = mysql_safe_string($_GET['id']); 
    } 

    $result = mysql_safe_query('SELECT * FROM posts WHERE id=%s LIMIT 1',$current_id); 
    if(!mysql_num_rows($result)){ 
    echo '<h2>No Posts Found</h2>'; 
    exit; 
    } 

    $row = mysql_fetch_assoc($result) 
    echo '<h2>'.$row['title'].'</h2>'; 
    echo '<div class="row">'; 
    echo ' <div class="group1 col-sm-6 col-md-6">'; 
    echo ' <span class="glyphicon glyphicon-pencil"></span><a data-toggle="collapse" data-target="#comments" class"collapsed">'.$row['num_comments'].' Comments </a>'; 
    echo ' <span class="glyphicon glyphicon-time"></span>'.date('F j<\s\up>S</\s\up>, Y', $row['date']); 
    echo ' </div>'; 
    echo '</div>'; 
    echo '<br />'; 
    echo '<p class="lead">'.n12br($row['body']).'</p>'; 
    ?> 

    <div id="comments" class="collapse" > 
    <div class="well"> 
     <h4>Leave a comment</h4> 
     <?php echo '<form role="form" method="post" action="php/comment_add.php?id=($current_id)" class="clearfix">'; ?> 
     <div class="col-md-6 form-group"> 
      <label class="sr-only" for="name">Name</label> 
      <input type="text" class="form-control" id="name" placeholder="Name" required /> 
     </div> 
     <div class="col-md-6 form-group"> 
      <label class="sr-only" for="email">Email</label> 
      <input type="email" class="form-control" id="email" placeholder="Email" required /> 
     </div> 
     <div class="col-md-12 form-group"> 
      <label class="sr-only" for="content">Comment</label> 
      <textarea class="form-control" id="content" placeholder="Comment" required></textarea> 
     </div> 
     <div class="col-md-12 form-group text-right"> 
      <button type="submit" class="btn btn-primary">Submit</button> 
     </div> 
     </form> 
    </div> 
    <br> 

<?php 
    $result = mysql_safe_query('SELECT * FROM comments WHERE post_id=%s ORDER BY date ASC',$current_id); 
    echo ' <ul id="comments" class="comments">'; 

    while($row = mysql_fetch_assoc($result)){ 
    echo ' <li class="comment">'; 
    echo '  <div id="inline" ><h4 style="display:inline;">'.$row['name'].'</h1><sup><p style="display:inline; font-size:10px;">&nbsp; '.date('j-M-Y g:ia', $row['date']).'</p></sup></div>'; 
    echo '  <em>'.n12br($row['content']).'</em>'; 
    echo ' </li>'; 
    echo ' </ul>'; 
    } 
?> 
    <hr> 
    </div> 
</div> 

<nav> 
    <ul class="pager"> 
<?php 

    $newer_id = IFNULL(mysql_safe_query('SELECT min(id) FROM posts WHERE id > $current_id ORDER BY id ASC LIMIT 1'),-1); 
    $older_id = IFNULL (mysql_safe_query('SELECT max(id) FROM posts WHERE id < $current_id ORDER BY id ASC LIMIT 1'),-1); 

    if($newer_id != -1){ 
     echo '<li><a href="#">Newer</a></li>'; 
    } 
    if ($older_id != -1){ 
     echo '<li><a href="#">Older</a></li>'; 
    } 
?> 
    </ul> 
</nav> 

這PHP/mysql_connect.php,這是爲了防止SQL注入(我得到這個從一個教程):

<?php 
// mysql.php 
function mysql_safe_string($value) { 
    $value = trim($value); 
    if(empty($value))   return 'NULL'; 
    elseif(is_numeric($value)) return $value; 
    else      return "'".mysql_real_escape_string($value)."'"; 
} 

function mysql_safe_query($query) { 
    $args = array_slice(func_get_args(),1); 
    $args = array_map('mysql_safe_string',$args); 
    return mysql_query(vsprintf($query,$args)); 
} 

function redirect($uri) { 
    header('location:'.$uri); 
    exit; 
} 

mysql_connect('localhost','(username)','(password)'); 
mysql_select_db('(database)'); 

從我找到故障日誌,其是:

Syntax error, unexpected 'max' (T_STRING) on line 6 (if(empty($_GET)){$current_id = SELECT max(id) FROM posts}) 
+2

如果您正在獲取'500',則應該有某個日誌。發佈它的相關部分將是第一步,弄清楚你面對什麼樣的錯誤。第6行 – h7r 2015-02-17 22:36:49

+0

語法錯誤,意想不到的 '最大'(T_STRING)(如果(空($ _ GET)){$ current_id = SELECT MAX(ID)來自帖子}) – xSpartanCx 2015-02-17 23:22:31

+1

您使用的舊的輔導。 'mysql_ *'函數(稱爲'ext/mysql')已棄用,不應使用。相反,你應該使用['PDO'(http://php.net/manual/en/book.pdo.php)或['mysqli'](http://php.net/manual/en/book.mysqli .PHP)。 – prodigitalson 2015-02-17 23:35:53

回答

0

如果你的文件是myfile.html,並且你沒有嵌入式PHP的權限,它將不會在服務器上執行。

1
if(empty($_GET)){ 
    $current_id = SELECT max(id) FROM posts; 
} 

sql周圍沒有引號是錯誤的。

1

至於其他指出的(它應該是立即語法錯誤,你所面臨的明確),你$current_id查詢不被引用。一個好的開始是修復第一個塊,因此:

if(empty($_GET)) { 
    $current_id = "SELECT max(id) FROM posts;"; 
} else { 
    $current_id = mysql_safe_string($_GET['id']); 
} 
+0

啊,我明白了。謝謝。如果他們是單引號還是雙引號,這有關係嗎? – xSpartanCx 2015-02-18 01:37:16

+0

在這種情況下,沒有。 – h7r 2015-02-18 09:28:58