php+html 表单提交数据保存到mysql中

4 篇文章 0 订阅
1 篇文章 0 订阅

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>";
?>

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值