我有一个包含4个项目的html表单。
title
和content
是必需的。
link
和image
是可选的。
所以这就是我写的,但是我无法在我的数据库中插入数据。 在我的条件声明中是否存在网址和图片错误?感谢
if($_SERVER['REQUEST_METHOD'] == "POST") {
$ttt = strlen(htmlspecialchars(trim($_POST['title'])));
$ccc = strlen(htmlspecialchars(trim($_POST['content']))); // count title and content charters
$title = htmlspecialchars(trim($_POST['title']));
$content = htmlspecialchars(trim($_POST['content']));
$url = htmlspecialchars(trim($_POST['url']));
$image = htmlspecialchars(trim($_POST['image']));
if($url!=''){
if ( !preg_match( '|^(ht|f)tp(s?))://|', $url ){
echo "wrong";
mysql_close($db);
}
} // if $url is not empty, judge is it began as a http:// ? else close the db link
if($image!=''){
if ( !getimagesize($image)){
echo "wrong";
mysql_close($db);
}
} // if $image is not empty, use getimagesize judge is it a real image? else close the db link
if(($ttt > 2 && $ttt < 201) && ($ccc > 29 && $ccc < 1001)) {
$sql= "INSERT INTO msg (title, content,image,link) VALUES ('".$title."','".$content."', '".$image."', '".$url."')";//if title charters between 3-200 and content charters between 30-1000, do insert into thing
if(mysql_query($sql))
{
echo "insert done";
}else{
echo "insert wrong";
}
}else{
echo "your title or content is out of the words limit";
}
}
mysql_close($db);
答案 0 :(得分:0)
似乎有一个mysql_close_db调用可能是问题?如果没有,请在mysql_query调用之前发布echo $ sql的结果,以及您收到的任何错误或错误。
答案 1 :(得分:0)
变化:
if($link!=''){
if ( !preg_match( '|^(ht|f)tp(s?))://|', $url ){
echo "wrong";
mysql_close($db);
}
}
到
if($url !=''){
if ( !preg_match( '|^(ht|f)tp(s?))://|', $url ){
echo "wrong";
mysql_close($db);
}
}
让我们能够帮助你更好地展示你的$ sql语句;
更新:
很难找到问题,就像你可以做的那样,看看问题是什么:
$ result = mysql_query($ sql)或die(&#39;错误:&#39; mysql_error());
通过这种方式,您可以查看查询中的错误,我非常确定这是一个逃避问题,所以这里是您可以添加的内容;
$title = htmlspecialchars(trim($_POST['title']));
$content = htmlspecialchars(trim($_POST['content']));
$url = htmlspecialchars(trim($_POST['url']));
$image = htmlspecialchars(trim($_POST['image']));
$title = mysql_real_escape_string($title);
$content = mysql_real_escape_string($content);
$url = mysql_real_escape_string($url);
$image = mysql_real_escape_string($image);
这样,如果有任何特殊字符,他们将被转义