日期:2014-05-17  浏览次数:20485 次

新手问查询数据库
数据库里有一个表,名字是content   ,表有四个字段title(text),url(text),content(text),signature(text).

有一个表单(名字是content)和数据库里的表对应。
<form   action= "a.php "   method= "post "   name= "myform ">
title: <input   type= "text "   name= "title "   size=50>
url: <input   type= "text "   name= "url "   size=50>
content: <textarea   type= "text "   name= "content "   rows=10,cols=55> </textarea>
signature: <input   type= "text "   name= "title "   size=50>
<input   type= "submit "   name= "submit "   value= "submit ">
我在表单里输入数据,先判断字段url不许为空,也不能和数据里已有的url字段内容重复。  
怎样写一个这样的查询句子?

------解决方案--------------------
在action页面的脚本中添加

if $_POST( "url ")== " "
{
echo "URL不能为空! ";
}

在表单页,可以使用javascript脚本进行判断不过要在form中添加 submit项

<script>
funciont checkform(obj){
if (document.all.myform.url.value== " "){alert( "url不能为空! ");return false;}
}
</ascript>
------解决方案--------------------
<html>
<body>
<?php
$url=$_POST[ 'url '];

$conn = mysql_connect( "localhost ", "root ", " ");
mysql_select_db( "test ", $conn);
$query = "select * from content where url= '$url ' ";
$result=mysql_query($query, $conn);
$row = mysql_fetch_array($result);

if(($row[ 'url '] == "$url ")||($url== " "))
{
print 'Your url is already exist,please enter other url,or url is empty ';
}
else
{
$query = "INSERT INTO content (id,title, url, content,signature,ip,timestamp) VALUES ( ' ', '{$_POST[ 'title ']} ', '{$_POST[ 'url ']} ', '{$_POST[ 'content ']} ', '{$_POST[ 'signature ']} ', '{$_SERVER[ 'REMOTE_ADDR ']} ', 'time() ') ";
$result=mysql_query($query);

print ' <p> new page had add ' ;
}
mysql_close();
?>
<form action= " " method= "post " name= "myform ">
title: <input type= "text " name= "title " size=50>
url: <input type= "text " name= "url " size=50>
signature: <input type= "text " name= "title " size=50>
content: <textarea type= "text " name= "content " rows=10,cols=55> </textarea>
<input type= "submit " name= "submit " value= "submit ">
</form>
</body>
</html>