新手问查询数据库数据库里有一个表,名字是content ,表有四个字段title(text),url(text),content(text),signature(text). 有一个表单(名字是content)和数据库里的表对应。 title: url: content: signature: 我在表单里输入数据,先判断字段url不许为空,也不能和数据里已有的url字段内容重复。 怎样写一个这样的查询句子?------解决方案--------------------在action页面的脚本中添加 if $_POST( "url ")== " " { echo "URL不能为空! "; } 在表单页,可以使用javascript脚本进行判断不过要在form中添加 submit项 <br /> funciont checkform(obj){ <br /> if (document.all.myform.url.value== " "){alert( "url不能为空! ");return false;} <br /> } <br /> <br /><font color='#e78608'>------解决方案--------------------<br /> <html> <br /> <body> <br /> <?php <br /> $url=$_POST[ 'url ']; <br /> <br /> $conn = mysql_connect( "localhost ", "root ", " "); <br /> mysql_select_db( "test ", $conn); <br /> $query = "select * from content where url= '$url ' "; <br /> $result=mysql_query($query, $conn); <br /> $row = mysql_fetch_array($result); <br /> <br /> if(($row[ 'url '] == "$url ")||($url== " ")) <br /> { <br /> print 'Your url is already exist,please enter other url,or url is empty '; <br /> } <br /> else <br /> { <br /> $query = "INSERT INTO content (id,title, url, content,signature,ip,timestamp) VALUES ( ' ', '{$_POST[ 'title ']} ', '{$_POST[ 'url ']} ', '{$_POST[ 'content ']} ', '{$_POST[ 'signature ']} ', '{$_SERVER[ 'REMOTE_ADDR ']} ', 'time() ') "; <br /> $result=mysql_query($query); <br /> <br /> print ' <p> new page had add ' ; <br /> } <br /> mysql_close(); <br /> ?> <br /> <form action= " " method= "post " name= "myform "> <br /> title: <input type= "text " name= "title " size=50> <br /> url: <input type= "text " name= "url " size=50> <br /> signature: <input type= "text " name= "title " size=50> <br /> content: <textarea type= "text " name= "content " rows=10,cols=55> <br /> <input type= "submit " name= "submit " value= "submit "> <br />