创建数据库和用户
CREATE DATABASE mysite;
grant all on mysite.* to mysite@localhost identified by '123456';
flush privileges;create database onbing;
grant all on onbing.* to onbing@localhost identified by 'h123TYBcd';
flush privileges;对于已存在的数据库和用户,使用如下方法:
首先确认数据库(为空)和用户;
use mysql
select user from user where User='onbing';
update user set password=password('h123TYBcd') where User='onbing';
grant all on onbing.* to onbing@localhost;
flush privileges;在本地hosts文件中添加dns解析
192.168.1.24 www.onbing.com
建表:
CREATE TABLE user(id int(5) DEFAULT '1' NOT NULL AUTO_INCREMENT, name varchar(20), city varchar(20), postcode varchar(10),address varchar(100), password varchar(10), level tinyint(2) DEFAULT '0',right tinyint(2),email varchar(40), PRIMARY KEY (id), UNIQUE id (id));CREATE TABLE news(id int(5) DEFAULT '1' NOT NULL , title varchar(100), dates datetime, contents varchar(200), PRIMARY KEY (id), UNIQUE id (id));
alter table news modify id integer auto_increment;INSERT INTO myuser VALUES (1,'王友','北京','100083','北京市海淀区学院路',111,0,0,'wangyou000@263.net');
INSERT INTO myuser VALUES (2,'金风','武汉','430071','武汉市武昌区水果湖',111,0,0,'jinfeng000@263.net');
INSERT INTO myuser VALUES (3,'常丽','襄樊','441021','襄樊市襄城区南街',111,0,0,'changli000@263.net');
1.链接数据库通用方法:conn.php
<?php
//第一步:链接数据库
$conn=@mysql_connect("localhost:3306","root","123456")or die ("mysql链接失败");
//第二步: 选择指定的数据库,设置字符集
@mysql_select_db("mysite",$conn) or die ("db链接失败".mysql_error());
mysql_query('SET NAMES UTF8')or die ("字符集设置错误");
?>2.增加 add.php
<?php
include("conn.php");//引入链接数据库
if(!empty($_POST['sub'])){
$title=$_POST['title'];
$con=$_POST['con'];
echo $sql="insert into news(title,dates,contents) value ('$title',now(),'$con')" ;
mysql_query($sql);
echo"插入成功";
}
?>
<form action="add.php" method="post">
标题: <input type="text" name="title"><br>
内容: <textarea rows="5" cols="50" name="con"></textarea><br>
<input type="submit" name="sub" value="发表">
</form>3.删除 del.php
<?php
include("conn.php");//引入链接数据库
if(!empty ($_GET['id'])){
$sql="select * from news where id='".$_GET['id']."'";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
}
?>
<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['contents']?></textarea><br>
<input type="submit" name="sub" value="发表">
</form>
</pre><br>
<?php
if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql);
echo "删除成功";
?>
<p></p>
<pre></pre>
<br>
4.改 edit.php页面
<p></p>
<p><br>
</p>
<p></p><pre name="code" class="html">
<?php
include("conn.php");//引入链接数据库
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',contents='$con' where id='$hid' limit 1 ";
mysql_query($sql);
echo "<script> alert('更新成功'); location.href='select.php'</script>";
echo"更新成功";
}
?>
<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['contents']?></textarea><br>
<input type="submit" name="sub" value="发表">
</form>
</pre><br>
5.查询页面 select.php
<pre name="code" class="html"><a href="add.php">添加内容</a>
<hr>
<hr>
<form>
<input type="text" name="keys" />
<input type="submit" name="subs" value="搜索"/>
</form>
<?php
include("conn.php");//引入链接数据库
if(!empty($_GET['keys'])){
$w=" title like '%".$_GET['keys']."%'";
}else{
$w=1;
}
$sql="select * from news where $w order by id desc";
$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 $rs['contents'] ?></p>
<hr>
<?php
}
?>
</pre><br>
<p></p>
<p><br>
</p>
1.查寻全部
<html>
<head>
<title> 查寻全部</title>
</head>
<body>
<h2 align=center>
下面是用户数据库的所有内容:
</h2>
<?
$db = MySQL_connect("localhost", "root");
MySQL_select_db("mysite",$db);
$result = MySQL_query("SELECT * FROM user",$db);
echo "<table border=1>\n";
echo "<tr><td>id</td><td>姓名</td>";
echo "<td>城市</td><td>邮编</td>";
echo "<td>住 址</td><td>等级</td>";
echo "<td>权限</td><td>邮件地址</td>";
echo "</tr>\n";
while ($myrow = MySQL_fetch_row($result))
{
printf("<tr><td>%s</td><td>%s</td>", $myrow[0], $myrow[1]);
printf("<td>%s</td><td>%s</td>", $myrow[2], $myrow[3]);
printf("<td>%s</td><td>%s</td>", $myrow[4], $myrow[6]);
printf("<td>%s</td><td>%s</td></tr>\n", $myrow[7], $myrow[8]);
}
echo "</table>\n";
?>
</body>
</html>2.根据id查询
<html>
<body>
<title>
根据id查询
</title>
<?
$db = MySQL_connect("localhost", "root");
MySQL_select_db("mysite",$db);
if ($id) {
$result = MySQL_query("SELECT * FROM user WHERE id=$id",$db);
$myrow = MySQL_fetch_array($result);
printf("<h2 align=left>用户的情况</h2>");
printf("1.ID号 %s\n<br>", $myrow["id"]);
printf("2.姓名: %s\n<br>", $myrow["name"]);
printf("3.城市: %s\n<br>", $myrow["city"]);
printf("4.邮编: %s\n<br>", $myrow["postcode"]);
printf("5.住址: %s\n<br>", $myrow["address"]);
printf("6.等级:%s\n<br>", $myrow["level"]);
printf("7.邮件地址: %s\n<br>", $myrow["email"]);
printf("<a href=\"%s\"> 继续查询 </a>", $PATH_INFO);
}
else
{
$result = MySQL_query("SELECT * FROM user",$db);
if ($myrow = MySQL_fetch_array($result))
{
printf("<h2 align=left>请选择要查询的用户:</h2>");
do {
printf("%s<a href=\"%s?id=%s\">%s </a><br>\n",
$myrow["id"],$PATH_INFO,$myrow["id"], $myrow["name"]);
}
while ($myrow = MySQL_fetch_array($result));
}
else
{
echo "对不起,没有数据!";
}
}
?>
</body>
</html>3.更新
<html>
<body>
<title>
更新
</title>
<?
//PHP程序的开始
$db = MySQL_connect("localhost", "root");
MySQL_select_db("mysite",$db);
if (!$id)
{
$result = MySQL_query("SELECT * FROM user",$db);
if ($myrow = MySQL_fetch_array($result))
{
printf("<h2 align=left>请选择要修改的用户名字:</h2>");
do {
printf("%s<a href=\"%s?id=%s\">%s </a><br>\n",
$myrow["id"],$PATH_INFO,$myrow["id"], $myrow["name"]);
}
while ($myrow = MySQL_fetch_array($result));
}
else
{
echo "对不起!数据库中没有记录";
}
}
else
{
if ($submit)
{
$sql = "UPDATE user SET name='$name',city='$city',
address='$address',email='$email' WHERE id=$id";
$result = MySQL_query($sql);
echo "谢谢!数据更改完成\n";
}
else
{
$sql = "SELECT * FROM user WHERE id=$id";
$result = MySQL_query($sql);
$myrow = MySQL_fetch_array($result);
//以下不是PHP的部分
?>
<h2 align=left>请对用户的信息进行修改:</h2>
<form method="post" action="<? echo $PATH_INFO ?>">
<input type=hidden name="id"
value="<?php echo $myrow["id"] ?>">
姓名:<input type="Text" name="first" value="<?php
echo $myrow["name"] ?>"><br>
城市:<input type="Text" name="city" value="<?php echo
$myrow["city"] ?>"><br>
住址:<input type="Text" name="address" value="<?php echo
$myrow["address"] ?>"><br>
电子邮件:<input type="Text" name="email" value="<?php echo
$myrow["email"] ?>"><br>
<input type="Submit" name="submit" value="修改完毕">
</form>
<?
//PHP 的结束
65:}
}
?>
</body>
</html>4.增加删除记录
<html>
<body>
<title>
增加删除记录
</title>
<?
$db = MySQL_connect("localhost", "root");
MySQL_select_db("mysite",$db);
&nb, sp;
if ($submit)
{
$sql = "INSERT INTO myuser (name,city,address,email)
VALUES ('$name','$city','$address','$email')";
$result = MySQL_query($sql);
echo "记录添加成功!<p>";
}
else
if ($delete)
{
// 删除一条记录
$sql = "DELETE FROM myuser WHERE id=$id";
$result = MySQL_query($sql);
echo "记录删除成功!<p>";
}
else
{
printf("<h2 align=left>删除一个用户的所有信息:</h2>");
$result = MySQL_query("SELECT * FROM myuser",$db);
while ($myrow = MySQL_fetch_array($result))
{
printf("%s:<a href=\"%s?id=%s&delete=yes\">%s</a> <br>\n",
$myrow["id"],$PATH_INFO, $myrow["id"], $myrow["name"]);
}
?>
<P>
<h2 align=left>
添加一个新的用户:
</h2>
<form method="post" action="<?php echo $PATH_INFO?>">
姓名:<input type="Text" name="name" value="<?php
echo $myrow["name"] ?>"><br>
城市:<input type="Text" name="city" value="<?php echo
$myrow["city"] ?>"><br>
住址:<input type="Text" name="address" value="<?php echo
$myrow["address"] ?>"><br>
电子邮件:<input type="Text" name="email" value="<?php echo
$myrow["email"] ?>"><br>
<input type="Submit" name="submit" value="添加用户">
</form>