Home >Backend Development >PHP Tutorial >新手问查询数据库解决方案

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

WBOY
WBOYOriginal
2016-06-13 10:02:01851browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn