我有一个包含4个项目的html表单。
title和content是必需的。
link和image是可选的。
这就是我所写的,但是我不能在我的数据库中插入数据。我的url和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);发布于 2011-05-10 01:51:30
更改:
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 ('Error:'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);这样,如果有任何特殊字符,它们将被转义
发布于 2011-05-10 01:51:01
似乎其中一个mysql_close_db调用可能是问题所在?如果没有,请在mysql_query调用之前发布回显$sql的结果,以及您收到的任何错误。
https://stackoverflow.com/questions/5940507
复制相似问题