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

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

Jun 13, 2016 pm 01:53 PM
content nbsp quot url

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

How to adjust window border settings on Windows 11: Change color and size

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

How to change title bar color on Windows 11?

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

How to enable or disable taskbar thumbnail previews on Windows 11

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

OOBELANGUAGE Error Problems in Windows 11/10 Repair

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

Display scaling guide on Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

10 Ways to Adjust Brightness on Windows 11

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

How to turn off private browsing authentication for iPhone in Safari?

See all articles