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");
  }
}
?>
4

3 回答 3

1

你的查询是错误的。

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

一定是

mysql_query("INSERT INTO pensieve(uid, subject, memory, dateline, private, links) values('$userID', '$title', '$entry', '".date()."', '$private', '$links')") or die(mysql_error());
于 2012-07-28T20:32:34.630 回答
1

首先在脚本顶部设置以下内容

  error_reporting(E_ALL);

这将告诉您有关哪里出了问题以及在哪条线上的所有信息。

其次,我不明白你为什么要这样做

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

您同时使用 INSERT 和 UPDATE 语法

如果你想插入使用

 "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();
    }
于 2012-07-28T20:42:51.527 回答
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'";
于 2014-03-13T19:38:50.333 回答