在php入门教程中php mysql是少不了的,下面我们就要告诉各位关于php mysql数据删除,更新,插入保存数据代码下面来一一看实例.
一、数据删除,代码如下:
- <?php
-
- $link_id = mysql_connect("localhost","root","") or die("连接失败");
- if($link_id)
- {
- mysql_select_db("my_test");
- if(!$_GET[id])
- {
-
- $result=mysql_query("select * from userinfo");
- echo "<TABLE border=1>
- <TR>
- <TD>编号www.111cn.net</TD>
- <TD>用户名称</TD>
- <TD>性别</TD>
- <TD>年龄</TD>
- <TD>注册时间</TD>
- <TD>操作</TD>
- </TR>";
-
-
-
- while($row=mysql_fetch_array($result)){
- echo "<TR>
- <TD>".$row[id]."</TD>
- <TD>".$row[username]."</TD>
- <TD>".$row[gender]."</TD>
- <TD>".$row[age]."</TD>
- <TD>".$row[regdate]."</TD>
- <TD><a href=delete.php?id=".$row[id].">删除</a></TD>
- </TR>";
- }
- echo "</TABLE>";
-
- }
- else
- {
-
- $sql="delete from userinfo where id=".$_GET[id];
- $result=mysql_query($sql);
- if($result)
- echo "记录已经成功删除<br><a href='delete.php'>返回</a>";
- else
- echo "记录删除失败<br><a href=delete.php.php?id=".$_GET[id].">返回</a>";
-
- }
- }
- ?>
二、php数据修改,代码如下:
- <?php
-
- $link_id = mysql_connect("localhost","root","") or die("连接失败");
- if($link_id)
- {
- mysql_select_db("my_test");
- if(!$_GET[id])
- {
-
- $result=mysql_query("select * from userinfo");
- echo "<TABLE border=1>
- <TR>
- <TD>编号</TD>
- <TD>用户名称</TD>
- <TD>性别</TD>
- <TD>年龄</TD>
- <TD>注册时间</TD>
- <TD>操作</TD>
- </TR>";
-
-
-
- while($row=mysql_fetch_array($result)){
- echo "<TR>
- <TD>".$row[id]."</TD>
- <TD>".$row[username]."</TD>
- <TD>".$row[gender]."</TD>
- <TD>".$row[age]."</TD>
- <TD>".$row[regdate]."</TD>
- <TD><a href=modify.php?id=".$row[id].">编辑www.111cn.net</a></TD>
- </TR>";
- }
- echo "</TABLE>";
-
- }
- else
- {
- if(!$_POST[ok])
- {
- $sql="select * from userinfo where id=".$_GET[id];
- $result=mysql_query($sql);
- $row=mysql_fetch_array($result);
- ?>
- <form method=post action='modify.php?id=<? echo $_GET[id];?>'>
- <?
- echo $row[id]."<br>";
- ?>
- <INPUT TYPE="hidden" name="id" value=<?echo $row[id];?>>
- 姓名 <input type=text name="username" value=<?echo $row[username];?>><br>
- 性别 <input type=text name="gender" value=<?echo $row[gender];?>><br>
- 年龄 <input type=text name="age" value=<?echo $row[age];?>><br>
- 注册时间 <input type=text name="regdate" value=<?echo $row['regdate'];?>><br>
- <input type=submit name=ok value="提交">
- </form>
- <?
- }
- else{
-
- $sql="update userinfo set username='".$_POST[username]."',gender='".$_POST[gender]."',age='".$_POST[age]."',regdate='".$_POST[regdate]."' where id='".$_POST[id]."'";
- $result=mysql_query($sql);
- if($result)
- echo "记录已经成功修改<br><a href='modify.php'>继续修改记录www.111cn.net</a>";
- else
- echo "记录修改失败<br><a href=modify.php?id=".$_POST[id].">返回</a>";
- }
- }
- }
- ?>
三、php数据保存,代码如下:
- <?php
- if($_POST[ok])
- {
- $link_id = mysql_connect("localhost","root","") or die("连接失败");
- if($link_id)
- {
-
- mysql_select_db("my_test");
-
- $sql="insert into userinfo values('".$_POST[id]."','".$_POST[name]."','".$_POST[gender]."','".$_POST[age]."','".$_POST[regdate]."')";
-
- $result=mysql_query($sql);
- if($result)
- {
- echo "记录已经成功插入<br><a href='insert.php'>继续插入记录</a>";
- }
- else
- echo "执行插入SQL语句失败";
-
- mysql_close($link_id);
- }
- }
- else
- {
- ?>
- <form method=post action=insert.php>
- 编号<input type=text name="id"><br>
- 姓名<input type=text name="name"><br>
- 性别<input type=text name="gender" ><br>
- 年龄<input type=text name="age"><br>
- 注册时间<input type=text name="regdate"><br>
-
- <input type=submit name=ok value="提交">
- </form>
- <?
- }
- ?>
|