首页  >  文章  >  后端开发  >  新手问查询数据库解决方案

新手问查询数据库解决方案

WBOY
WBOY原创
2016-06-13 13:53:41748浏览

新手问查询数据库
数据库里有一个表,名字是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项

<script> <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[ &#39;url &#39;]; <br /> <br /> $conn = mysql_connect( "localhost ", "root ", " "); <br /> mysql_select_db( "test ", $conn); <br /> $query = "select * from content where url= &#39;$url &#39; "; <br /> $result=mysql_query($query, $conn); <br /> $row = mysql_fetch_array($result); <br /> <br /> if(($row[ &#39;url &#39;] == "$url ")||($url== " ")) <br /> { <br /> print &#39;Your url is already exist,please enter other url,or url is empty &#39;; <br /> } <br /> else <br /> { <br /> $query = "INSERT INTO content (id,title, url, content,signature,ip,timestamp) VALUES ( &#39; &#39;, &#39;{$_POST[ &#39;title &#39;]} &#39;, &#39;{$_POST[ &#39;url &#39;]} &#39;, &#39;{$_POST[ &#39;content &#39;]} &#39;, &#39;{$_POST[ &#39;signature &#39;]} &#39;, &#39;{$_SERVER[ &#39;REMOTE_ADDR &#39;]} &#39;, &#39;time() &#39;) "; <br /> $result=mysql_query($query); <br /> <br /> print &#39; <p> new page had add &#39; ; <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 /> </script>

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn