2013-12-23 35 views
-1

我設法向評論表添加評論,但它不會顯示在相關文章中。我理解,因爲文章表中的外鍵(artID)是空的。如何從文章表中獲取主鍵?這是我的數據庫的結構。如何添加評論以發佈到Mysql和PHP

SQL注入問題將在稍後處理。準備好的陳述將完成。我只想獲得一些關於查詢和php函數的幫助。謝謝。

文章
artid的數據類型
artTitre
artAuteur
artContenu
artDate

住客評論
commentID
commentPseudo
commentText
artid的數據類型

commentaires.sql.php

<?php 

    // INSERT 
    function insertCommentaire($c){ 

$PseudoCommentaire = $TexteCommentaire =''; 


$PseudoCommentaire = $_POST['PseudoCommentaire']; 
$TexteCommentaire = $_POST['TexteCommentaire']; 
    $IdArticle = $_POST['IdArticle']; 


$qryInsertComm = 'INSERT INTO commentaires (commentPseudo,commentText, artID) 
        VALUES (\''.$PseudoCommentaire.'\', 
          \''.$TexteCommentaire.'\', 
          \''.$IdArticle.'\') 
         '; 


if (!mysqli_query($c,$qryInsertComm)) 
{ 
die('Error: ' . mysqli_error($c)); 
} 
echo "1 record added"; 

} 


// UPDATE 
function updateCommentaire($IdCommentaire){ 


} 


// DELETE 
function deleteCommentaire($IdCommentaire){ 


} 


// CONTROLER // 


switch($action){ 

case 'insert' : 

    $process = insertCommentaire($conn); 

    if($process == 'ok') 
     header('location:index.php?page=home'); 
    else 
     $page = 'home'; 
    break; 

case 'update' : 
    $process = updateCommentaire($_GET[ 'item' ]);  
    if($process == 'ok') 
     header('location:index.php?page=home'); 
    else 
     $page = 'home'; 
    break; 

case 'delete' : 
    $process = deleteCommentaire($_GET[ 'item' ]);  
    if($process == 'ok') 
     header('location:index.php?page=home'); 
    break; 
    } 

    ?> 
+4

這裏有很多代碼。請隔離該問題併發布相關代碼 –

回答

0

您正在使用一個隱藏的輸入VAL稱爲IdArticle

<input type="hidden" name="IdArticle" value="" /> 

爲了跟蹤這些文章的評論應該去反對,因爲在PHP腳本如上圖所示:

$IdArticle = $_POST['IdArticle']; 

然而,看着你的HTML,你應該看到你正在給你的PHP腳本傳遞一個空值。

要解決這個問題,更新您的隱藏輸入字段是這樣的:

<input type="hidden" name="IdArticle" value="<?php echo $rows['artID'] ?>" /> 

現在,它應該工作。

+0

謝謝!你做到了! – swissed

+0

輸出爲HTML時請使用['htmlspecialchars'](http://php.net/htmlspecialchars)以防[XSS](https://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%29 )。 –

0

如何獲取從文章表的主鍵?

等同於選擇任何其他列

SELECT `artID` FROM `articles` WHERE `artID` = ? 

這就是我可以在你的帖子看到的唯一問題。