/ 我想用表单插入数据并为其编写简单的代码。 Html部分运行良好。在数据库中创建了post表,但此代码无法在数据库中插入数据。请帮帮我 / 插入新帖子
</head>
<body>
<form method="post" action="insert_post_1.php" enctype="multipart/form-data">
<table width="600" align="center" border="10">
<tr>
<td align="center" bgcolor="yellow" colspan="6">
<h1>Insert New Post Here</h1>
</td>
</tr>
<tr>
<td align="right">Post Title:</td>
<td><input type="text" name="title" size="30"></td>
</tr>
<tr>
<td align="right">Post Author:</td>
<td><input type="text" name="author" size="30"></td>
</tr>
<tr>
<td align="right">Post Keywords:</td>
<td><input type="text" name="keywords" size="30"></td>
</tr>
<tr>
<td align="right">Post Image:</td>
<td><input type="file" name="image" size="30"></td>
</tr>
<tr>
<td align="right">Post Content:</td>
<td><textarea name="content" cols="30" rows="25"></textarea></td>
</tr>
<tr>
<td align="center" colspan="6"><input type="submit" name="submit" value="Publish Now"></td>
</tr>
</table>
</form>
</body>
</html>
/ html没有问题它工作得很好但是使用这个PHP代码我无法检测到问题。单击“发布”按钮时,不会将数据插入数据库 /
if($post_title == '' or $post_keywords == '' or $post_author == '' or $post_content == ''){
echo "<script>alert('Any of the fields is empty')</script>";
exit();
}
else{
move_uploaded_file($post_temp, "images/$post_image");
/*this part of code is not running and hence unable to insert data to database*/
$insert_query = "INSERT INTO "
. "posts (`post_id`, `post_title`, `post_date`, `post_author`, `post_image`, `post_keywords`, `post_content`)"
. " VALUES (NULL, $post_title, $post_date, $post_author, $post_image, $post_keywords, $post_content)";
if(mysql_query($insert_query)){
echo "<center><h1>Post Published Successfully</h1></center>";
}
}
}
?>
答案 0 :(得分:0)
如果您仍想使用mysql扩展,请注释此块
if(mysql_query($insert_query)){
echo "<center><h1>Post Published Successfully</h1></center>";
}
并在下方添加
mysql_query($insert_query) or die(mysql_error());
echo "<center><h1>Post Published Successfully</h1></center>";
以查看错误
答案 1 :(得分:-1)
首先,您需要查看THIS文档:
mysql_query
已被删除。使用PDO
或mysqli
。
检查mysql中的错误:
try{
// Run your query:
}
catch(Exception $e){
echo $e->getMessage();
}