php mysq資料編輯更新執行個體

來源:互聯網
上載者:User
 代碼如下 複製代碼

$db = mysql教程_connect("localhost", "phpdb", "phpdb");
mysql_select_db("test",$db);

// 如果提交了submit按鈕
if ($submit) {  
   // 如果沒有id,則是在增加記錄,否則是在修改記錄
   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')";
   }  
   // 向資料庫教程發出sql命令
   $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 {  
   // 如果還沒有按submit按鈕,那麼執行下面這部分程式
   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"];
     // 顯示id,供使用者編輯修改
     ?>
     <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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.