本文章是也一款入门级php教程了,主要是告诉你如何利用php mysq数据编辑更新,代码如下:
- $db = mysql_connect("localhost", "phpdb", "phpdb");
- mysql_select_db("test",$db);
-
- if ($submit) {
-
- if ($id) {
- $sql = "update employees set first='$first',last='$last', address='$address',position='$position' where id=$id";
- }
- else {
- $sql = "insert into employees (first,last,address,position) values ('$first','$last','$address','$position')";
- }
-
- $result = mysql_query($sql);
- echo "记录修改成功!<>";
- echo "<a href='$php_self'>返回</a>";
- }
- elseif ($delete) {
-
- $sql = "delete from employees where id=$id";
- $result = mysql_query($sql);
- echo "记录删除成功!<>";
- echo "<a href='$php_self'>返回</a>";
- }
- else {
-
- if (!$id) {
-
- $result = mysql_query("select * from employees",$db);
- while ($myrow = mysql_fetch_array($result)) {
- printf("<a href="%s?id=%s">%s %s</a> ",
- $php_self, $myrow["id"], $myrow["first"], $myrow["last"]);
- printf("<a href="%s?id=%s&delete=yes">(delete)</a><br>", $php_self, $myrow["id"]);
- }
- }
- ?>
- <a href="<?php echo $php_self?>">返回</a>
- <form method="post" action="<?php echo $php_self?>">
- <?php
- if ($id) {
-
- $sql = "select * from employees where id=$id";
- $result = mysql_query($sql);
- $myrow = mysql_fetch_array($result);
- $id = $myrow["id"];
- $first = $myrow["first"];
- $last = $myrow["last"];
- $address = $myrow["address"];
- $position = $myrow["position"];
-
- ?>
- <input type=hidden name="id" value="<?php echo $id ?>">
- <?php
- }
- ?>
- 名:<input type="text" name="first" value="<?php echo $first ?>">
- 姓:<input type="text" name="last" value="<?php echo $last ?>">
- <br>
- 住址:<input type="text" name="address" value="<?php echo $address ?>">
- 职位:<input type="text" name="position" value="<?php echo $position ?>">
- <br>
- <input type="submit" name="submit" value="输入信息">
- </form>
- <?php
- }
- ?>
- </body>
- </html>
|