所以我累了,现在已经尝试了好几个小时,仍然无法找到它为什么不能工作。了解我,这可能是我在某处犯过的一些愚蠢的错误。 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 :(得分: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());
答案 1 :(得分: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();
}
答案 2 :(得分: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'";