以下是我在表格中添加数据的HTML表单:
<div class="TTWForm-container">
<div id="form-title" class="form-title field"><h2>ახალი სტატია</h2></div>
<form enctype="multipart/form-data" action="index.php?link=addart" class="TTWForm ui-sortable-disabled" method="post">
<div id="field2-container" class="field f_100 ui-resizable-disabled ui-state-disabled">
<label for="title">სათაური</label>
<input name="title" id="title" required="required" type="text">
</div>
<div id="field10-container" class="field f_100 ui-resizable-disabled ui-state-disabled">
<label for="post">
სტატია
</label>
<textarea rows="15" cols="40" name="post" id="post" required="required"></textarea>
</div>
<div id="form-submit" class="field f_100 clearfix submit">
<input value="დამატება" type="submit" id="submit">
</div>
</form>
</div>
处理
if (isset($_POST['title'])) {$title = $_POST['title'];}
if (isset($_POST['post'])) {$post = $_POST['post'];}
<?php
if (isset($title) && isset($post))
{
/*can add post */
$insert = "INSERT INTO article ('title','post') VALUES ('$title','$post')";
$result = mysqli_query($connect,$insert);
if ($result == 'true') {echo "<p>post is added.</p>";}
else {echo "<p>post is not added.</p>";}
}
else
{
echo "<p>Fill all fields.</p>";
}
?>
处理后总是说没有添加帖子。检查数据库及其真实性。无法找到错误。请帮忙。感谢。
答案 0 :(得分:2)
这是为您准备的声明。
$link = new MySQLi('localhost','username','password','database');
if(isset($_POST['title'],$_POST['post'])&& $_POST['title'] !="" && $_POST['post'] !=""){
$title = $_POST['title'];
$post = $_POST['post'];
if($query = $link->prepare('INSERT INTO article (title,post) VALUES(?,?)')){
$query->bind_param('ss',$title,$post);
$query->execute();
$query->close();
echo 'Success!';
}else{
echo 'Failure!';
}
}else{
echo 'You missed a field!';
}
答案 1 :(得分:1)
布尔比较
首先,检查您的比较。 mysqli_query
返回布尔值true
或false
,而不是字符串&#39; true&#39;或者&#39; false。&#39;而不是:
if ($result == 'true')
使用
if ($result == true)
甚至:
if ($result)
调试查询
接下来,检查查询本身是否有错误。作为Fred noted,您应该使用后标记而不是单引号标记列名:
INSERT INTO article (`title`,`post`)
考虑记录mysqli_error的输出,或尝试回显查询并针对数据库手动运行它。您可能会发现它们的查询具有隐藏的语法或数据错误,这在代码中并不明显。
发布值
接下来,检查从$_POST
获取的值。 $_POST['post']
设置了吗?它有用吗?
数据库连接
最后,检查数据库连接本身。你真的联系了吗?您是否在连接尝试周围进行了调试记录或输出?
答案 2 :(得分:0)
所以只需改变:
else
{
echo "<p>post is not added</p>";
}
为:
else
{
echo "<p>MySQL error:".mysqli_error($connect)."</p>";
}