2016-05-16 28 views
-2

我想我不能發佈超過2個鏈接,因爲我是新的,所以我會把鏈接放在pastebin上。 http://pastebin.com/2kab4LND隨機表單不提交數據庫值

基本上,我寫了一個表單將值提交給我的數據庫。這些值是暱稱,標題,日期和內容(這基本上是他們寫東西的地方) 無論如何,幾個小時前,這是完美的工作。無論內容值的文本數量多少,都會提交到數據庫。

第一張圖顯示數據庫按預期工作。

但是,現在當我嘗試使用表單來提交與該表單相同的值時,它將不會顯示出來並且沒有錯誤。第二張照片大致顯示了我可以在沒有顯示內容之前獲得內容。其他一切都有效,比如暱稱和標題,只是一旦它超出了我想要的限制,這些內容就不起作用。

我沒有改變我的任何代碼,所以我會告訴你我的MySQL表後。

第三張圖片顯示了我的表格。我沒有改變他們的任何事情。 桌子看起來像他們完美的工作。

這是我的表單代碼。我刪除了一些東西,所以閱讀起來稍微容易一些,但這樣做會很好。這是之前工作過的代碼,我沒有改變它,所以我不確定是否會有任何錯誤導致這種情況發生。現在我會告訴你它連接的是什麼。對不起,它真的很混亂。

<?php 

echo "<form id='submit-form' name='submit-form' action='".setDreams($conn)."' method='POST'> 
    <input type='hidden' name='dreamid' value=''> 
    <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'> 
    <div id='title'> 
    <textarea type='text' name='dreamtitle' value='title' id='title_textarea' placeholder='Write the title of your dream here. (Max 40 characters)' method='POST' rows='1' style='margin-left: 10px' style='z-index: 1'></textarea> 
    </div> 
    <div id='dream_editor'> 
    <textarea type='text' name='thedream' value='dream' id='dream_textarea' placeholder='Describe your dream here (Must be at least 250 characters)' method='POST' rows='8' style='margin-left: 10px' style='z-index: 1'></textarea> 
    </div> 
    <div id='desired_nickname'> 
    <textarea type='text' method='POST' type='nickname' name='dreamnickname' value='' id='nickname_textarea' placeholder='Write your desired nickname' rows='1' style='margin-left: 10px' style='z-index: 1'></textarea> 
    </div> 
    <button id='submitDreamButton' style='margin-bottom: 10px' name='dreamSubmit' type='submit'></button> 
    <button id='cancelButton' style='margin-bottom: 10px' href='index.php'></button> 

    </form>"; 
    ?> 

這是從窗體中刪除的函數。無論如何,正如我已經說過,這是有效的代碼,直到我沒有改變它之後停止工作。我查了很多遍,沒有發現任何錯誤。

<?php 
function setDreams($conn) { 
    if (isset($_POST['dreamSubmit'])) { 
     $uid = $_POST['dreamid']; 
     $date = $_POST['date']; 
     $dreamtitle = $_POST['dreamtitle']; 
     $thedream = $_POST['thedream']; 
     $nickname = $_POST['dreamnickname']; 

     $sql = "INSERT INTO dreams (dreamid, nickname, date, content, title) VALUES ('$uid','$nickname','$date','$thedream','$dreamtitle')"; 
     $result = $conn->query($sql); 
    } 
} 

這是強調我,因爲我知道它只是工作,我很困惑,爲什麼它現在不工作。我希望這篇文章是不是太難看,我希望你明白的問題:)

在此先感謝

+0

這裏添加所有代碼,代碼不是圖像 – 2016-05-16 03:45:59

+0

對不起啊,我會做到這一點 – jertyu

+0

我添加的代碼。 – jertyu

回答

0

action屬性必須是URL格式。

<form id='submit-form' name='submit-form' action='process.php' method='POST'> 
    <input type='hidden' name='dreamid' value=''> 
    <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'> 
      <div id='title'> 
     <textarea type='text' name='dreamtitle' value='title' id='title_textarea' placeholder='Write the title of your dream here. (Max 40 characters)' method='POST' rows='1' style='margin-left: 10px' style='z-index: 1'></textarea> 
    </div> 
    <div id='dream_editor'> 
     <textarea type='text' name='thedream' value='dream' id='dream_textarea' placeholder='Describe your dream here (Must be at least 250 characters)' method='POST' rows='8' style='margin-left: 10px' style='z-index: 1'></textarea> 
    </div> 
    <div id='desired_nickname'> 
     <textarea type='text' method='POST' type='nickname' name='dreamnickname' value='' id='nickname_textarea' placeholder='Write your desired nickname' rows='1' style='margin-left: 10px' style='z-index: 1'></textarea> 
    </div> 
    <button id='submitDreamButton' style='margin-bottom: 10px' name='dreamSubmit' type='submit'></button> 
    <!-- Your submit button must have the value attribute otherwise, isset($_POST['dreamSubmit']) means your button doesn't have a value --> 
    <button id='submitDreamButton' style='margin-bottom: 10px' name='dreamSubmit' value="Submit" type='submit'></button> 
    <button id='cancelButton' style='margin-bottom: 10px' href='index.php'></button> 

</form> 

process.php

<?php 
if (isset($_POST['dreamSubmit'])) { 
    setDreams($conn); 
} 

function setDreams($conn) { 
    $uid = $_POST['dreamid']; 
    $date = $_POST['date']; 
    $dreamtitle = $_POST['dreamtitle']; 
    $thedream = $_POST['thedream']; 
    $nickname = $_POST['dreamnickname']; 

    $sql = "INSERT INTO dreams (dreamid, nickname, date, content, title) VALUES ('$uid','$nickname','$date','$thedream','$dreamtitle')"; 
    $result = $conn->query($sql); 
} 
+0

強制性的:如果你使用原始用戶輸入作爲變量進入數據庫查詢,你將會被黑客入侵。 – 2016-05-16 03:55:41

+0

我有一個名爲dreams.inc.php的文件,其中包含我在帖子底部顯示的功能。我使用了「include'dreams.inc.php',然後在我的表單中調用它,這是不是工作?因爲它幾個小時前工作。 – jertyu

+0

@jertyu沒有辦法,工作,根本不能這樣。也許代碼當時沒有功能? – 2016-05-16 03:56:41