標籤:names 表單 bsp pos mail query 建立 cal img
利用PHP的post方式將擷取到的資料寫入mysql資料庫中。
首先建立一個用於接收使用者資料的表單頁面 denglu.html
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<form action="./xinxi.php" method=‘POST‘>
<p>使用者名稱:<input type="text" name="username" value="請輸入手機號碼"></p>
<p>性 別:<input type="radio" name="sex" value="man" >先生<input type="radio" name=‘sex‘ value="woman">女士</p>
<p>出生年月:<input type="text" name="year" value="1990">年<input type="text" name="yue" value="01">月<input type="text" name="ri" value="01">日</p>
<p>郵 箱:<input type="text" name="email"></p>
<p>密 碼:<input type="password" name="password"></p>
<p>確認密碼:<input type="password" name="password"></p>
<p><input type="submit" value="Login"></p>
</form>
接著建立一個用於載入 denglu.html 的Login.php
<?php
include ‘./denglu.html‘;
建立一個能接收使用者填寫資料的,並且能使用語句操作mysql的 xinxi.php,該php代碼是用於denglu.html中action指定的處理表單資料的伺服器。
1 <?php 4 $birth =$_POST[‘year‘].$_POST[‘yue‘].$_POST[‘ri‘]; 5 $pass = md5($_POST[‘password‘]); 6 7 $flag=mysql_connect(‘localhost‘,‘root‘,‘******‘); 8 $f=mysql_query(‘use test1‘); 9 $f=mysql_query(‘set names utf8‘);
10 $conn="insert into yonghu(username,sex,birth,email,password) values(‘{$_POST[‘username‘]}‘,‘{$_POST[‘sex‘]}‘,‘{$birth}‘,‘{$_POST[‘email‘]}‘,‘{$pass}‘);";11 if($_POST){12 $username = isset($_POST[‘username‘]) ? $_POST[‘username‘] :‘‘;13 if(isset($username)){14 mysql_query($conn);15 echo "insert into yonghu(username,sex,birth,email,password) values(‘{$_POST[‘username‘]}‘,‘{$_POST[‘sex‘]}‘,‘{$birth}‘,‘{$_POST[‘email‘]}‘,‘{$pass}‘);";16 echo "<hr />傳入資料成功!";17 die;21 }22 }
效果
php向mysql寫入資料