簡體   English   中英

如何將post_id插入mysql數據庫

[英]How do I insert an post_id into a mysql database

我有一個注釋系統,應該輸入一個id,idea_id,一個user_id,一個注釋,數據和時間。 除了我每次發表評論外,所有內容似乎都可以正常運行,idea_id始終為0。

我這樣做是使用:

<?php
    if(isset($_POST['submit'])) {

        $comment = $_POST['comment'];
        $user_id = $_SESSION['user_id'];
        $idea_id = $_POST['idea_id'];


        if(empty($comment)) {
            $message = "You Haven't Written Anything";
    } else {
        mysql_query("INSERT INTO comments (idea_id, user_id, comment, date, time) VALUES('".$idea_id."', '".$user_id."', '".$comment."', now(), now()) ") or die (mysql_error());
        $message = "OK! Thanks for leaving your comment!";  
        if(isset($_GET['user_id'])) {
    $_SESSION['user_id'] = $_GET['user_id'];
    }

    }   

        echo "<div class = 'box'>$message</div>";
    }
    ?>  



<form method = 'Post'  name = 'comment_form'>

    Comment: <br/>
    <input type = 'text' name = 'comment' id = 'comment' autocomplete= 'off' />
    <input type = 'hidden' name = 'idea_id' value = '<?php echo $idea_id; ?>' />
    <input type = 'submit' name = 'submit' value = 'Comment' /> 
</form> 

您應該在表單中定義$idea_id ,然后將其傳遞給表單

 $idea_id = "your_idea_id";  
 <input type = 'hidden' name = 'idea_id' value = '<?php echo $idea_id;?>' />

如果您想查看是否可行,請嘗試使用任何數字

  <input type = 'hidden' name = 'idea_id' value = '66' />

您似乎並沒有對正在通過表單的想法不斷進行計數。

我建議您允許數據​​庫為您管理idea_id,它是主鍵。

您的應用程序將如何處理重復的idea_id?

隱藏輸入的值應在php標記內:

<input type = 'hidden' name = 'idea_id' value = '<?php echo $idea_id;?>' />

這樣,查詢就不會將idea_id視為字符串。

同樣,如echo_Me所述,您應該在某個位置定義變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM