80 likes | 201 Views
PHP and MySQL. Chin-Yi Tsai cyt@pmlab.iecs.fcu.edu.tw http://140.134.26.25/~cyt. Example. account.php insert.php update.php delete.php search.php. insert into account values($new_id, '$name'). update account set name = '$name' where id =$id. delete from account where id =$id.
E N D
PHP and MySQL Chin-Yi Tsai cyt@pmlab.iecs.fcu.edu.tw http://140.134.26.25/~cyt
Example • account.php • insert.php • update.php • delete.php • search.php insert into account values($new_id, '$name') update account set name = '$name' where id =$id delete from account where id =$id select * from account where name = '$name'
<?php $link = mysql_connect("localhost", “root", “ "); $select = mysql_select_db("test_db", $link); $sql_select = "select * from account"; $result = mysql_query($sql_select); ?> <html> <title>PHP and Mysql Example</title> <body> <center> <table border =1> <tr> <td>編號 <td>姓名 <?php while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row[0]; echo "<td>" . $row[1]; } mysql_free_result($result); ?> account.php
</table> <hr> <form action="insert.php" method="post"> 姓名<input type=text name=name size=20> <input type=submit value="新增"> </form> <hr> <form action="update.php" method="post"> 編號<input type=text name=id size=5> 姓名<input type=text name=name size=20> <input type=submit value="修改"> </form> <hr> <form action="delete.php" method="post"> 編號<input type=text name=id size=5> <input type=submit value="刪除"> </form> <hr> <form action="search.php" method="post"> 姓名<input type=text name=name size=20> <input type=submit value="搜尋"> </form> </center> </body> </html> account.php
insert.php <?php $link = mysql_connect("localhost", "root", " "); $select = mysql_select_db("test_db", $link); $id = $_POST['id']; $name = $_POST['name']; $sql_update = "update account set name = '$name' where id =$id"; mysql_query($sql_update); header("Location: http://leo.pmlab.iecs.fcu.edu.tw/PHP_example/PHP_cyt/account.php"); ?>
update.php <?php $link = mysql_connect("localhost", "root", " "); $select = mysql_select_db("test_db", $link); $id = $_POST['id']; $name = $_POST['name']; $sql_update = "update account set name = '$name' where id =$id"; mysql_query($sql_update); header("Location: http://127.0.0.1/account.php"); ?>
delete.php <?php $link =mysql_connect("localhost", "root", " "); $select = mysql_select_db("test_db", $link); $id = $_POST['id']; $sql_delete = "delete from account where id =$id"; mysql_query($sql_delete); header("Location: http://127.0.0.1/account.php"); ?>
search.php <?php $link = mysql_connect("localhost", "root", " "); $select = mysql_select_db("test_db", $link); $name = $_POST['name']; $sql_search = "select * from account where name = '$name'"; $result = mysql_query($sql_search); ?> <html> <body> <?php $row = mysql_fetch_array($result); echo "搜尋結果<br>"; echo "編號:".$row[0]."<br>"; echo "姓名:".$row[1]."<br>"; ?> <br> <a href="account.php">返回</a> </body> </html>