Maison >développement back-end >tutoriel php >php表单插入数据库,求大神帮忙
php数据库
<code><?php if(!isset($_POST['submit'])){ //如果没有表单提交,显示一个表单 ?> <form action="" method="post"> <table> <tr> <td>请输入姓名:<input type="text" name="username"> </td> </tr> <tr> <td>请输入电话: <input type="text" name="mobile"> </td> </tr> <tr> <td>请确认城市: <input type="text" name="city"> </td> </tr> <tr> <td>请输入邮箱:<input type="text" name="emile"> </td> </tr> <tr> <td> <input type="submit" value="报名"> <input type="reset" value="重置"> </td> </tr> </table> </form> <?php } else { //如果提交了表单 //数据库连接参数 $host = "localhost"; $user = "root"; $pass = ""; $db = "anxinjia"; //取得表单中的值,检查表单中的值是否符合标准,并做适当转义,防止SQL注入 $username=$_POST['username']; $mobile=$_POST['mobile']; $city=$_POST['city']; $emile=$_POST['emile']; //打开数据库连接 $connection = mysql_connect($host, $user, $pass) or die("Unable to connect!"); //选择数据库 mysql_select_db($db) or die("Unable to select database!"); //构造一个SQL查询 $query = "INSERT INTO shuipf_AttractInvestment(username, mobile, city,emile) VALUES('$username', '$mobile', '$city','$emile')"; //执行该查询 $result = mysql_query($query) or die("Error in query: $query. ".mysql_error()); //插入操作成功后,显示插入记录的记录号 echo "记录已经插入, mysql_insert_id() = ".mysql_insert_id(); //关闭当前数据库连接 mysql_close($connection); } ?></code>