php+mysql實現(xiàn)簡單的增刪改查功能
更新時間:2015年07月13日 11:27:33 投稿:hebedich
本文給大家分享的是使用php結合mysql實現(xiàn)簡單的增刪改查的功能的代碼,非常的簡單實用,有需要的小伙伴可以參考下。
列表代碼
<?php $con = mysql_connect("localhost:3306","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT * FROM user"); echo "<table border='1'> <tr> <th>Username</th> <th>Password</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['username'] . "</td>"; echo "<td>" . $row['password'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?>
刪除
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); mysql_query("DELETE FROM user WHERE username = '$_POST[username]'"); mysql_close($con); ?>
添加
<?php $con = mysql_connect("localhost:3306","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $sql = "INSERT INTO user (username,password) VALUES ('$_POST[username]','$_POST[password]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con); ?>
修改
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); mysql_query("UPDATE user SET password = '$_POST[password]' WHERE username = '$_POST[username]'"); mysql_close($con); ?>
<html> <head> <title>用PHP向數(shù)據(jù)庫中實現(xiàn)簡單的增刪改查</title> </head> <body> <br /> <h1>Insert:</h1> <form action="insert.php" method="post"> username:<input type="name" name="username"/> <br /> password:<input type="password" name="password"/> <input type="submit" value="submit"/> </form> <br /><hr /><br /> <h1>Delete</h1> <form action="delete.php" method="post"> username:<input type="name" name="username" /> <br /> Are you sure?<input type="submit" value="sure" /> </form> <br /><hr /><br /> <h1>Update</h1> <form action="update.php" method="post"> username:<input type="name" name="username"/> <br /> You want to change your password into:<input type="password" name="password"/> <input type="submit" value="submit"/> </form> <br /><hr /><br /> </body> </html>
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關文章
PHP反射類ReflectionClass和ReflectionObject的使用方法
PHP反射類ReflectionClass和ReflectionObject2013-11-11PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
這篇文章主要介紹了PHP創(chuàng)建XML的方法,結合實例形式分析了php基于DOMDocument類及SimpleXMLElement類創(chuàng)建xml文件的具體步驟與相關實現(xiàn)技巧,需要的朋友可以參考下2019-09-09php通過baihui網(wǎng)API實現(xiàn)讀取word文檔并展示
這篇文章主要介紹了php通過baihui網(wǎng)API實現(xiàn)讀取word文檔并展示的相關資料,需要的朋友可以參考下2015-06-06使用PHPMailer實現(xiàn)郵件的實時發(fā)送功能
這篇文章主要為大家詳細介紹了如何使用PHPMailer 實現(xiàn)一個接收詢盤并實時同步到指定郵箱的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-12-12PHP實現(xiàn)找出鏈表中環(huán)的入口節(jié)點
這篇文章主要介紹了PHP實現(xiàn)找出鏈表中環(huán)的入口節(jié)點,涉及php針對環(huán)形鏈表的遍歷、查找、計算等相關操作技巧,需要的朋友可以參考下2018-01-01PHP數(shù)組相加操作及與array_merge的區(qū)別淺析
這篇文章主要給大家介紹了關于PHP數(shù)組相加操作以及與array_merge的區(qū)別,文中通過示例介紹的很詳細,感興趣的朋友們可以參考學習,有需要的下面跟著小編一起來學習學習吧。2016-11-11