表单插入数据,需要两个文件,第一个addshoes.php
以下文件使用boot框架
<?php include 'dblink.php'; //引入数据库连接文件 ?> <html> <head> <meta http-equiv="Content-Type" content="text/html" charset="utf-8" /> <title>添加女鞋数据</title> </head> <?php include 'header.php'; //头部引入的css文件提出来?> <body> <div class="main"> <div class="form-horizontal"> <div class="form-group"> <label class="col-sm-2 control-label">商品名</label> <div class="col-sm-4"> <input type="text" class="form-control" name="picname" placeholder="请填写主图名"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">商品价</label> <div class="col-sm-4"> <input type="text" class="form-control" name="jdprice" placeholder="请填写京东价"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">邮费</label> <div class="col-sm-4"> <input type="text" class="form-control" name="postfree" placeholder="请填写邮费"> </div> </div> <hr> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" class="btn btn-primary" id="submit_btn" style="padding:4px 20px;" vlaue="提交"> </div> </div> </div> </div> <?php include 'footer.php'; //引入的js文件 ?> <script> $(document).ready(function(){ $("#submit_btn").click(function(){ var picname = $("input[name='picname']").val(); var jdprice = $("input[name='jdprice']").val(); var postfree = $("input[name='postfree']").val(); $.ajax({ type: 'POST', url: "addAction.php", data: {"picname":picname,"jdprice":jdprice,"postfree":postfree}, dataType: 'json', success: function(result){ if(result == 1){ alert("添加成功"); }else{ alert("添加失败"); } window.location.href = 'http://localhost/shoes/shoesmanage.php' } }); }); }) </script> </body> </html>
这里没有写是否判断数据为空什么的,主要展示与数据库的交互
第二个文件addAction.php
<?php include 'dblink.php'; $picname=$_POST['picname']; $jdprice=$_POST['jdprice']; $postfree=$_POST['postfree']; $sql="insert into shoes_jd_wx(picname,jdprice,postfree)values('$picname','$jdprice','$postfree')"; //mysql_query($sql); //执行sql语句,插入数据。 if(mysql_query($sql)){ echo json_encode(1); }else{ echo json_encode(0); } mysql_close($dblink); ?>
如此,数据库就能插入一条数据了