這次我要分享的是php如何將表單中的信息寫入到數據庫中。以網頁的註冊界面爲例,註冊的話我們在網頁上填寫用戶名,密碼等信息,然後通過表單將信息傳入後臺服務器的數據庫表格中。在這過程中起到連接作用的就是php了。這裏我就省略了數據庫建表格這些過程。
<span style="font-size: 18px;"> </span><span style="font-size:14px;"> <form method="post" >
<table>
<tr>
<td>請輸入用戶名:<input type="text" name="username"></td>
</tr>
<tr>
<td>請輸入密碼: <input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="註冊"> </td>
</tr>
</table>
</form></span>
上面是用html寫的登入界面的設計,表單傳送有2種方法,我上面例子用的是POST。
?php
header("content-type:text/html;charset=utf-8");
session_start();
$name=$_POST['username'];
$pwd=$_POST['password'];
$conn=mysql_connect("localhost","","");
mysql_select_db("test");
mysql_query("set names utf8");
$sqlinsert="insert into t1(username,password) values('{$name}','{$pwd}')";
mysql_query($sqlinsert);
mysql_close($conn);
?>
這上面就是將表單數據寫入數據庫的代碼。