PHP之简单的数据库操作项目 发表于 2018-03-31 | 分类于 PHP 主界面是获取数据库里的相关内容:123456789101112131415161718192021222324<?phpheader("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代码:1234567891011121314151617181920212223242526272829<?phpheader("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代码:12345678910111213141516171819202122232425262728<?phpheader("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代码部分:12345678910111213141516171819202122232425262728293031323334<?phpheader("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);?>