2011-05-09 37 views
1

我有一个包含4个项目的html表单。php mysql插入条件判断

titlecontent是必需的。

linkimage是可选的。

所以这是我写的,但我不能在我的数据库中插入数据。 我的条件声明中的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); 
+0

哪里链接从何而来?也许它应该是$ url而不是$ link – Ibu 2011-05-09 17:49:39

+0

如果我们能够看到'$ sql'的实际值,那将是有帮助的,否则我会说这是你的问题,你实际上没有写任何SQL。 – Endophage 2011-05-09 17:51:04

+0

@Ibrahim Diallo,谢谢。这是一个错误,但我再次尝试,仍然没有工作。 – cj333 2011-05-09 17:52:53

回答

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语句;

UPDATE:

这是很难推测一样,这里的问题是,你可以做什么,看看是什么问题:

$结果= mysql_query($的SQL)或死亡(“错误:'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); 

这样,如果有任何特殊字符,它们将被转义

+0

@David Fells,@Ibrahim Diallo,@Endophage,我已经更新了我的'$ sql',并且已经将'$ link'更改为'$ url'。这是我的写错。谢谢。但改变后,仍然没有插入完成。 – cj333 2011-05-09 18:01:23

+0

好吧,我更新了我的代码这种方式我敢肯定它会工作:) – Ibu 2011-05-09 18:14:55

+0

Diallo,现在的代码是工作。但我不知道为什么'mysql_close($ db);'不起作用。甚至是一个错误的网址。它仍然插入数据。所以我使用'$ title ='';'而不是'mysql_close($ db);'以便停止插入数据。谢谢。 – cj333 2011-05-09 18:50:04

0

似乎可能是mysql_close_db调用之一可能是问题?如果没有,请在mysql_query调用之前发布回显$ sql的结果,以及您收到的任何错误或错误。