所以我很累,已经尝试了几个小时了,仍然找不到为什么它不起作用。了解我,这可能是我在某个地方犯的愚蠢错误。总之,我有一个表格,我需要能够提交,然后输入的文本被插入数据库。然而,每当我按下提交按钮时,它只会返回主页,不插入任何内容。代码在这里:
//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");
}
}
?>
您的查询是错误的。
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());
首先在脚本的顶部设置以下内容
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();
}
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'";