PHP之简单的数据库操作项目

主界面是获取数据库里的相关内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
header("content-type:text/html;charset=utf-8");
$line = mysqli_connect("localhost","root","");
if(!$line){
exit("open my_sql is false");
}
mysqli_set_charset($line,"utf8");
$res=mysqli_select_db($line,"acc");
$str="select * from pro1";
$result=mysqli_query($line,$str);
echo '<table width="600" border="1">';
echo '<th>编号</th><th>用户名</th><th>资金</th><th>性别</th><th>操作</th>';
while($rows=mysqli_fetch_assoc($result)){
echo '<tr>';
echo '<td>'.$rows['id'].'</td>';
echo '<td>'.$rows['name'].'</td>';
echo '<td>'.$rows['age'].'</td>';
echo '<td>'.$rows['sex'].'</td>';
echo '<td><a href="del.php?id='.$rows['id'].'">删除</a><a href="change.php?id='.$rows['id'].'">修改</a></td>';
echo '</tr>';
}
echo "</table>";
?>

有关删除的php代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
header("content-type:text/html;charset=utf-8");
$id=$_GET['id'];
$line=mysqli_connect("localhost","root","");
if(!$line){
exit("连接数据库失败");
}
mysqli_set_charset($line,"utf8");
mysqli_select_db($line,"acc");
$str="delete from pro1 where id = $id";
$res=mysqli_query($line,$str);
if($res && mysqli_affected_rows($line)){
echo '输出成功<a href="sql1.php">返回</a>';
}else{
echo "删除失败";
}
?>

修改的相关PHP代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
header("content-type:text/html;charset=utf-8");
$id=$_GET['id'];
$line=mysqli_connect("localhost","root","");
if(!$line){
exit("连接数据库失败");
}
mysqli_set_charset($line,"utf8");
mysqli_select_db($line,"acc");
$str="select * from pro1 where id = $id";
$res=mysqli_query($line,$str);
$rows=mysqli_fetch_assoc($res);
?>
<html>
<form action="do.php">
<input type="hidden" value="<?php echo$id;?>" name="id"/>
用户名:<input type="text" value="<?php echo $rows["name"]?>" name="username"/>
<input type="submit" value="执行修改"/>
</form>
</html>

有关提交的相关PHP代码部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
header("content-type:text/html;charset=utf-8");
var_dump($_GET);
$id=$_GET['id'];
$username=$_GET['username'];
//updata pro1 set name='333' where id =1 ;
$line =mysqli_connect("localhost","root","");
if(!$line){
exit("数据库连接失败");
}
mysqli_set_charset($line,"utf8");
mysqli_select_db($line,"acc");
$str="update pro1 set name='$username' where id = $id";
$res=mysqli_query($line,$str);
if($res && mysqli_affected_rows($line)){
echo '修改成功<a href="sql1.php">返回</a>';
}
else
{
echo "修改失败";
}
mysqli_close($line);
?>
Fork me on GitHub