我正在开发一个网站,涉及用户将他们最喜欢的内容发布到网页上,所有的数据都存储在数据库中,并在网站上显示。然而,每当我尝试使用我创建的表单和处理表单的php脚本发布大段落时,它不会给出任何错误,但也不会将值存储到数据库中。如果要处理的内容很短,则可以正常工作。请帮我看看下面的代码。如有任何建议,我们将不胜感激。
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "library";
$conn = mysql_connect($hostname,$username,$password)or die('Cannot connect');
mysql_select_db("library", $conn);
?>
<?php
$title = $_POST['title'];
$author = $_POST['author'];
$content = strval($_POST['content']);
$description = $_POST['description'];
$category = $_POST['category'];
if (isset($title) && !empty($title)){
$sql = "INSERT INTO books(title,author,description,content,category) VALUES('$title','$author','$description','$content','$category')";
mysql_query($sql);
echo 'Successfully Added the book'."<a href='/'><br>Check it out here<a>";
}
else{
echo 'Title is missing';
}
?>谢谢。
发布于 2015-12-05 07:39:10
首先,请使用PDO -如果没有,至少使用mysql_real_escape_string对POST参数进行转义
$title = mysql_real_escape_string($_POST['title']);
$author = mysql_real_escape_string($_POST['author']);
$content = mysql_real_escape_string($_POST['content']);
$description = mysql_real_escape_string($_POST['description']);
$category = mysql_real_escape_string($_POST['category']);此外,strval不足以转义$_POST['content']以便在SQL查询中使用,mysql_real_escape_string (如上所述)更好。
一旦你解决了这些问题:
有两个可能失败的地方(我能想到的)
https://stackoverflow.com/questions/34099193
复制相似问题