標籤:
<?phpheader("content-type:text/html;charset=utf-8");/*//造一個串連$connect = @mysql_connect("localhost","root","123");//選擇要操作的資料庫mysql_select_db("mydb",$connect);//寫SQL語句$sql = "select * from Nation";//執行SQL語句,返回結果集$result = mysql_query($sql);//從結果集中讀取資料while($attr = mysql_fetch_row($result)){ var_dump($attr);}*///造一個串連$connect = @mysql_connect("localhost","root","123");//選擇要操作的資料庫mysql_select_db("mydb",$connect);//寫SQL語句$sql = "insert into Nation values(‘n007‘,‘壯族‘)";//執行SQL語句,返回結果集$result = mysql_query($sql);//從結果集中讀取資料var_dump($result);//中文亂碼解決方案:原因編碼格式不一致//1.建立資料庫的時候,字元集選擇utf-8//2.修改mysql的配置:在[mysqld]模組下面添加character_set_server=utf8//3.修改頁面的編碼格式為uft-8,header("content-type:text/html;charset=utf-8
結構
<body><form action="chuli.php" method="post"><div>民族代號:<input type="text" name="code" /></div><div>民族名稱:<input type="text" name="name" /></div><input type="submit" value="提交" /></form></body><script type="text/javascript">confirm(確定刪除麼);</script></html>
例子
<?php$code = $_POST["code"];$name = $_POST["name"];//造串連$conn = @mysql_connect("localhost","root","123");//選擇操作的資料庫mysql_select_db("mydb",$conn);//寫SQL語句$sql = "insert into Nation values(‘{$code}‘,‘{$name}‘)";//執行$result = mysql_query($sql);if($result){ //跳轉頁面 header("location:lizi.php");}else{ echo "添加失敗!";}
處理
<?php$code = $_GET["code"];//造串連$conn = @mysql_connect("localhost","root","123");//選擇操作的資料庫mysql_select_db("mydb",$conn);//寫SQL語句$sql = "delete from Nation where Code = ‘{$code}‘";//執行$result = mysql_query($sql);if($result){ //跳轉頁面 header("location:chaxun.php");}else{ echo "刪除失敗!";}
刪除
<table width="100%" border="1" cellpadding="0" cellspacing="0"><tr> <td>民族代號</td> <td>民族名稱</td> <td>操作</td></tr><?php $conn = @mysql_connect("localhost","root","123"); //選擇操作的資料庫 mysql_select_db("mydb",$conn); //寫SQL語句 $sql = "select * from Nation"; //執行 $result = mysql_query($sql); //讀取資料,造option while($attr = mysql_fetch_row($result)) { echo "<tr> <td>{$attr[0]}</td> <td>{$attr[1]}</td> <td><a onclick=\"return confirm(‘確定刪除麼‘)\" href=‘delete.php?code={$attr[0]}‘>刪除</a></td> </tr>"; }?></table>
表格式查詢和是否刪除
php 簡單操作資料庫