php+html 表單提交數據保存到mysql中

html form端代碼

<div id="content">

<form method="post" action="php/regist.php" >
<table >
<tr>
<th>用戶名</th>
<td><input name="username" value="" placeholder="這裏輸入文字"  type="text" /></td>
</tr>
<tr>
<th>密碼</th>
<td><input name="password" value="" type="password"  placeholder="請設置登錄密碼" /></td>
</tr>
<tr>
<th>手機號</th>
<td>
<input name="phone" value="" type="text" placeholder="可用於登錄和找回密碼"/>
</td>

</tr>

<tr>

<th></th>
<td><input type="submit" value="註冊" name="reg" id="tijiao" /></td>
</tr>
</table>
</form>

</div>



php端代碼

<?php
//聲明變量並接受form表單發送過來的數據
$username = $_POST['username']; 
$password = $_POST['password'];
$phone = $_POST['phone'];

//字符串拼接,打印輸出
echo $username."<br/>".$password."<br/>".$phone."<br/>";

//連接數據庫
$con = mysql_connect("localhost","root","");
if($con){
echo "<br/>連接成功"."<br/>";
} else{
echo "<br/>連接失敗".mysql_error();
}
//選擇數據庫
mysql_select_db("html");
//設置mysql字符編碼
mysql_query("set names utf8;");
//insert語句
$insert = "insert into user (username,password,phone) values ('$username','$password','$phone')";
/*$res_insert = mysql_query($insert);
if($res_insert){
echo "<br/>插入成功";
} else {
echo "<br/>插入失敗";
}*/
//update語句
$update = "update user set username='$username',password='$password',phone='$phone' where id= 20";
//$res_update = mysql_query($update);
//select語句
$select = "select * from user";
$res_select = mysql_query($select);
echo "<table width=600 align='center'><tr><td>id</td><td>username</td><td>password</td><td>phone</td></tr>";
while($row = mysql_fetch_row($res_select)){
echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td><td>".$row[3]."</td></tr>";
}
echo "</table>";
?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章