本文主要和大家分享php实现向数据库表中插入数据,主要以代码的形式和大家分享,希望能帮助到大家。
php插入数据页面insertdata.php
<html>
<head>
<title>向数据库中插入数据</title>
</head>
<body>
<center>
<form name="myform" method="post" action="processinsertdata.php">
<table bgcolor="cyan">
<tr>
<td>学号</td>
<td><input type="text" name="txtid" /></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="txtname" /></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="txtage"></td>
</tr>
<tr>
<td>性别</td>
<td>
<select name="ssex">
<option value="male">male</option>
<option value="female">female</option>
</select>
</td>
</tr>
<tr>
<td>地址</td>
<td><input type="text" name="txtaddress"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="添加"/>
<input type="reset" name="reset" value="重置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
处理数据插入页面processinsertdata.php
<?php
$link=mysql_connect("localhost","root","root");
mysql_select_db("rorely");
$id=$_post['txtid']|null;
$name=$_post['txtname'];
$age=$_post['txtage'];
$sex=$_post['ssex'];
$address=$_post['txtaddress'];
$exec="insert into test values($id,'$name',$age,'$sex','$address')";
$result=mysql_query($exec);
if($result) echo "学生已添加到数据表中<br>";
else echo "该学生没有添加进数据表,请重新输入。<br>";
mysql_close();
?>
<a href="insertdata.php">返回添加页面</a>
相关推荐:
详解yii框架批量插入数据的简单扩展类
mysql批量插入数据如何优化的方法介绍
php多进程插入数据
以上就是php实现向数据库表中插入数据的详细内容。