標籤:串連資料庫 部落格
1、建立資料庫
CREATE TABLE news(id INT( 12 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,title VARCHAR( 50 ) ,dates DATE,connects TEXT,hits INT( 11 ) NOT NULL) ENGINE = INNODB DEFAULT CHARSET = utf8 AUTO_INCREMENT =1
2、串連資料庫conn.php
<?php@mysql_connect("127.0.0.2","root","")or die("mysql connect false");@mysql_select_db("wikidb")or die("database connect false");//mysql_set_charset("utf-8");mysql_query("set names ‘utf-8‘");?>
3、建立首頁index.php
<a href=‘add.php‘>新增內容</a><hr><form action="index.php" method="get"><input type="text" name="keys"><input type="submit" name="subs" value="search"></form><?phpinclude("conn.php");//conn datebase;if(!empty($_GET[‘keys‘])){ $w="`title` like ‘%".$_GET[‘keys‘]."%‘";}else{ $w=1;} $sql="SELECT * FROM `news` where $w order by id desc limit 10";$query=mysql_query($sql);while($rs=mysql_fetch_array($query)){?><h2>標題:<a href="view.php?id=<?php echo $rs[‘id‘]?>"><?php echo $rs[‘title‘]?></a>|<a href="edit.php?id=<?php echo $rs[‘id‘]?>">編輯</a>|<a href="del.php?del=<?php echo $rs[‘id‘]?>">刪除</a>|</h2><li><?php echo $rs[‘dates‘]?></li><p><?php echo iconv_substr($rs[‘connects‘],0,100,"")?><a href="view.php?id=<?php echo $rs[‘id‘]?>">[繼續閱讀]</a></p><hr><?php}?>
4、建立內容頁view.php
<?phpinclude("conn.php");//conn datebase;if(!empty($_GET[‘id‘])){ $sql="SELECT * FROM `news` WHERE `id`=‘".$_GET[‘id‘]."‘"; $query=mysql_query($sql); $rs=mysql_fetch_array($query); $sqlup="UPDATE `news` SET `hits`=hits+1 WHERE `id`=‘".$_GET[‘id‘]."‘"; mysql_query($sqlup);}?><h1><?php echo $rs[‘title‘]?></h1><h2><?php echo $rs[‘dates‘]?></h2><h3>點擊量:<?php echo $rs[‘hits‘]?></h3><hr><p><?php echo $rs[‘connects‘]?></p>
5、建立編輯頁edit.php
<?phpinclude("conn.php");//conn datebase;if(!empty($_GET[‘id‘])){ $sql="SELECT * FROM `news` WHERE `id`=‘".$_GET[‘id‘]."‘"; $query=mysql_query($sql); $rs=mysql_fetch_array($query);} if(!empty($_POST[‘sub‘])){ $title=$_POST[‘title‘]; $con=$_POST[‘con‘]; $hid=$_POST[‘hid‘]; $sql="UPDATE `news` SET `title`=‘$title‘,`connects`=‘$con‘ WHERE id=‘$hid‘ limit 1"; mysql_query($sql); echo "<script>alert(‘update successful!‘);location.href=‘index.php‘</script>";}?><form action="edit.php" method="post"><input type="hidden" name="hid" value="<?php echo $rs[‘id‘] ?>">標題<input type="text" name="title" value="<?php echo $rs[‘title‘] ?>"><br>內容<textarea rows="5" cols="50" name="con"><?php echo $rs[‘connects‘] ?></textarea><br><input type="submit" name="sub" value="發表"></form>
6、建立刪除頁del.php
<?phpinclude("conn.php");//conn datebase;if(!empty($_GET[‘del‘])){ $d=$_GET[‘del‘]; $sql="DELETE FROM `news` WHERE `id`=‘$d‘"; mysql_query($sql); echo "delete successful";}?>
本文出自 “Jerry” 部落格,請務必保留此出處http://alipay.blog.51cto.com/7119970/1537559