2012-07-28 176 views
0

所以我很累,一直在嘗試這個好幾個小時,現在仍然無法找到它爲什麼不起作用。瞭解我,這可能是我在某個地方犯的一個愚蠢的錯誤。 Anyhoo,我有一個表格,我需要能夠提交,隨後輸入的文本被插入到數據庫中。然而,每當我按下提交,它只是回到主頁面,並沒有插入任何東西。代碼在這裏:提交表單到數據庫不能正常工作

//New Memory 
<?php 
if ($x == 'new') { 
?> 

<a href="pensieve_elizabeth.php"><- Back</a> 
<center> 
<table width="400" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
     <td> 
      <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> 
      <input type="hidden" name="submitted" value="submitted" /> 

      <p><b>Add Memory </b></p> 
      <p>Title:<br> 
      <input class="textfield" name="title" maxlength="55" style="width:325px;"> 
      <br> 
      Memory: <br> 
      <textarea class="textfield" name="entry" value="entry" id="entry" cols="30" rows="10" style="width:325px;"></textarea><br> 

      <input type="submit" value=" Submit " /> 

      <input type="checkbox" name="private" value="1"> Private 

      <p>What thread does the memory belong to? (optional):<textarea class="textfield" name="links" rows="1" style="width:325px;"></textarea><br /> 

     </form> 
    </td> 
    </tr> 
</table> 
</center> 

<?php 

    if(isset($_POST['submitted'])) { 

    $title = trim(addslashes(strip_tags(htmlspecialchars($_POST['title'])))); 
    $entry = trim(addslashes(strip_tags(htmlspecialchars($_POST['entry'])))); 
    $private = $_POST['private']; 
    $links = $_POST['links']; 

    if (empty($title)) message("Please give the memory a title."); 
    if (empty($entry)) message("Your memory is empty."); 

    mysql_query("INSERT INTO pensieve SET uid = '$userID', subject = '$title', memory = '$entry', dateline = '".date()."', private = '$private', links = '$links'") or die(mysql_error()); 

    message("You have successfully submitted this memory to your pensieve!","/pensieve_elizabeth.php"); 
    } 
} 
?> 
+0

爲什麼回聲之前不動你isset塊? – funerr 2012-07-28 20:32:18

+0

啊是的,我之前有過,它沒有工作,所以我複製另一個腳本移動它:/ – Elizabeth 2012-07-28 20:33:39

+0

$ PHP_SELF只是指當前的網址 – Elizabeth 2012-07-28 20:34:03

回答

1

你的查詢是錯誤的。

mysql_query("INSERT INTO pensieve SET uid = '$userID', subject = '$title', memory = '$entry', dateline = '".date()."', private = '$private', links = '$links'") or die(mysql_error()); 

必須在腳本的頂部

error_reporting(E_ALL); 

這將告訴你什麼不順心,並在哪一行的一切以下

mysql_query("INSERT INTO pensieve(uid, subject, memory, dateline, private, links) values('$userID', '$title', '$entry', '".date()."', '$private', '$links')") or die(mysql_error()); 
+0

我怎麼錯過了,哦,上帝,我會編輯我的答案無論如何不錯 – 2012-07-28 20:43:34

+0

你以前的回答是更好的...'INSERT INTO ... SET'是[有效的MySQL語法](http://dev.mysql .COM/DOC/refman/5.5/EN/insert.html)。 – rid 2012-07-28 20:44:45

+0

現在編輯它,它仍然不起作用:/謝謝,但我會記住未來的格式! – Elizabeth 2012-07-28 20:45:13

1

先做設置。

其次,我不明白爲什麼你正在使用INSERT和UPDATE語法,你這樣做

INSERT INTO pensieve SET uid = '$userID', subject = '$title', 
memory = '$entry', dateline = '".date()."', 
    private = '$private', links = '$links'"); 

一起

,如果你想插入使用

"INSERT INTO pensieve ('uid', 'subject', 'memory', 'dateline', 
    'private', 'links') VALUES ('$userID','$title','$entry','".date()."', 
    '$private', '$links'"); 

的以下將直接插入一行,而不考慮單個行中的重複值

,如果你想更新使用

"UPDATE pensieve SET uid = '$userID', subject = '$title', memory = '$entry', 
    dateline = '".date()."', private = '$private', links = '$links'" 
    WHERE 'something' = 'something_value' "; 

共做

$result = mysql_query("INSERT INTO pensive......."); 
    if(!$result){ 
     mysql_error(); 
    } 
+0

我剛開始,當它沒有工作,我想我會嘗試這個,而不是。現在改回它,但它仍然不起作用 – Elizabeth 2012-07-28 20:44:28

+0

問題在你的代碼中的其他地方。如果你可以發佈你的'message(...)'方法,這將會很有幫助 – jmishra 2012-07-28 20:45:32

0
Add these two lines to the top of your script, this will show any sort of errors in the script or query execution: 

    ini_set("display_errors","on"); 
    error_reporting(E_ALL); 

    And for the data not getting inserted: 
    check your query if all the mandatory/not null values are passed in the query or not. For example - I do not see userid being set in your script. 

    Your insert query syntax is wrong - the correct syntax is 
insert into table1(col1,col2,col3) values(val1,val2,val3); 

so your query would be : 

$sql ="INSERT INTO pensieve(uid,subject,memory,dateline,private,links) values ('$userID','$title', '$entry','". date('Y-m-d H:i:s',time())."', '$private', '$links'"; 

if you want to update then : 

$SQL = "UPDATE pensieve SET uid = '$userID', subject = '$title', memory = '$entry', dateline = '".date('Y-m-d H:i:s',time())."', private = '$private', links = '$links'";