创建表语句:
1 CREATE TABLE IF NOT EXISTS `task` ( 2 `id` int( 11) NOT NULL AUTO_INCREMENT, 3 `name` varchar( 30) NOT NULL, 4 `tel` varchar( 30) NOT NULL, 5 `operate_date` date NOT NULL, 6 PRIMARY KEY (`id`) )
PHP脚本:
1 operate list: 2 <a href="curd.php?a=c">add</a> 3 / 4 <a href="curd.php?a=l"> list</a> 5 <br /> 6 <br /> 7 <?php 8 $action = "l"; 9 10 if ( isset( $_REQUEST["a"])) { 11 $action = $_REQUEST["a"]; 12 } 13 14 if ( $action == "c") { // add form 15 showCreateForm(); 16 } elseif ( $action == "u") { // update from 17 showUpdateForm(); 18 } elseif ( $action == "l") { // show list 19 showList(); 20 } elseif ( $action == "save") { // save 21 $name = $_REQUEST["name"]; 22 $tel = $_REQUEST["tel"]; 23 $operate_date = $_REQUEST["operate_date"]; 24 25 $sql = "insert into task(name,tel,operate_date) values(' $name',' $tel',' $operate_date') "; 26 executeSql( $sql); 27 echo "save success !<br/>"; 28 showList(); 29 } elseif ( $action == "update") { // update 30 $id = $_REQUEST["id"]; 31 $name = $_REQUEST["name"]; 32 $tel = $_REQUEST["tel"]; 33 $operate_date = $_REQUEST["operate_date"]; 34 35 $sql = "update task set name=' $name',tel=' $tel',operate_date=' $operate_date' where id=' $id' "; 36 executeSql( $sql); 37 echo "update success !<br/>"; 38 showList(); 39 } elseif ( $action == "d") { // delete 40 $id = $_REQUEST["id"]; 41 $sql = "delete from task where id=' $id'"; 42 executeSql( $sql); 43 echo "delete success !<br/>"; 44 showList(); 45 } 46 47 function executeSql( $sql){ 48 $db = getMysqlConn(); 49 $db->query( $sql); 50 $db->close(); 51 } 52 53 function getList( $sql) { 54 $db = getMysqlConn(); 55 $result = $db->query( $sql); 56 57 $num_results = $result->num_rows; 58 59 $data = array(); 60 for ( $i = 0; $i < $num_results ; $i++) { 61 $row = ( array) $result->fetch_assoc(); 62 array_push( $data, $row); 63 } 64 65 $result->free(); 66 $db->close(); 67 return $data; 68 } 69 70 function getMysqlConn(){ 71 @ $db = new mysqli("localhost","root","","galaxia_platform"); 72 if ( mysqli_connect_errno()) { 73 echo "Error: connect mysql failed"; 74 exit; 75 } 76 return $db; 77 } 78 79 function showCreateForm(){ 80 ?> 81 <form action="curd.php?a=save" method="post"> 82 name:<input type="text" name="name"><br /> 83 tel:<input type="text" name="tel"><br /> 84 operate_date:<input type="text" name="operate_date"><br /> 85 <input type="submit" name="submit"> 86 </form> 87 <? 88 } 89 90 function showUpdateForm(){ 91 $id = $_REQUEST ["id"]; 92 ?> 93 <form action="curd.php?a=update" method="post"> 94 <? 95 $sql = "select * from task where id = ' $id' "; 96 $data = getList( $sql); 97 foreach( $data as $task) { 98 ?> 99 <input type="hidden" name="id" value="<? echo $task["id"]?>"><br /> 100 name:<input type="text" name="name" value="<? echo $task["name"]?>"><br /> 101 tel:<input type="text" name="tel" value ="<? echo $task["tel"]?>"><br /> 102 operate_date:<input type="text" name="operate_date" value="<? echo $task["operate_date"]?>"><br /> 103 <input type="submit" name="submit"> 104 </form> 105 <? 106 } 107 } 108 109 function showList(){ 110 ?> 111 <table border="1"> 112 <tr> 113 <th>name</th> 114 <th>tel</th> 115 <th>operate_date</th> 116 <th>operate</th> 117 </tr> 118 <? 119 $sql = "select * from task"; 120 $data = getList( $sql); 121 foreach( $data as $task) { 122 ?> 123 <tr> 124 <td><? echo $task["name"]?></td> 125 <td><? echo $task["tel"]?></td> 126 <td><? echo $task["operate_date"]?></td> 127 <td><a href="curd.php?a=d&id=<? echo $task["id"]?>">del</a>/<a href="curd.php?a=u&id=<? echo $task["id"]?>">edit</a></td> 128 </tr> 129 <? 130 } 131 ?> 132 </table> 133 <? 134 }
135 ?>