首页 > 代码库 > php通过post将表单数据保存到数据库实例

php通过post将表单数据保存到数据库实例

html的form表单

技术分享
1 <form id="contact-form" method="POST" action="../php/msg.php">2       <input type="text" class="form-control" name="name" placeholder="name" id="name" required />3        <input type="email" class="form-control" name="email" placeholder="E-mail" id="email" required />4         <textarea class="form-control" name="message" rows="5" placeholder="message" id="messages" required></textarea>5         <button type="submit" class="btn btn-default">submit</button>6 </form>
View Code

php操作mysql的代码

技术分享
 1 <?php  2     $con=mysql_connect("127.0.0.1","root","root"); 3     if(!$con){ 4         die(mysql_error()); 5     } 6  7     mysql_select_db("message",$con); 8  9     $name=$_POST["name"];10     $email=$_POST["email"];11     $message=$_POST["message"];12     $msgdate=date(‘Y-m-d‘,time());13     $sql=mysql_query("insert into message(name,email,information,msgdate) values(‘$name‘,‘$email‘,‘$message‘,‘$msgdate‘)",$con);14     if(!$sql){15         die(mysql_error());16     }17 18     mysql_close($con);19 20     header("Location:../index.html#6");21 ?>
View Code

 

php通过post将表单数据保存到数据库实例