php+sql创建本地项目详细步骤4——编辑更新数据库数据

根据上一篇的管理页面,可以看出,编辑进入页面updateshoes.php

<?php include 'dblink.php';

$id=$_GET['id']; 

$sql="select * from shoes_jd_wx where id='$id'";

$rs=mysql_query($sql);

$row=mysql_fetch_array($rs);

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" /> 
<title>编辑女鞋数据</title>
</head>
<?php include 'header.php';?>
<body>
<div class="main">
<div class="current_nav"><span class="font16">编辑女鞋</span></div>
<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="请填写主图名" value="<?php echo $row['picname']; ?>">
        </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="请填写京东价" value="<?php echo $row['jdprice']; ?>">
        </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="请填写邮费" value="<?php echo $row['postfree']; ?>">
        </div>
    </div>
    <hr>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <input type="submit" class="btn btn-primary" id="edit_btn" style="padding:4px 20px;" vlaue="提交">
        </div>
    </div>
</div>
</div>
<?php include 'footer.php';?>
<script>
$(document).ready(function(){
    $("#edit_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: "updateAction.php?id=<?php echo $row['id']; ?>",
              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>

编辑后提交到updateAction.php

<?php include 'dblink.php';

$id=$_GET['id'];

$picname=$_POST['picname'];
$jdprice=$_POST['jdprice'];
$postfree=$_POST['postfree'];

$sql="update shoes_jd_wx set picname='$picname',jdprice='$jdprice',postfree='$postfree' where id='$id'";

//mysql_query($sql);


if(mysql_query($sql)){
    echo json_encode(1);
}else{
    echo json_encode(0);
}
mysql_close($dblink);
?>

这样,php+sql一个简单的增删该查项目便完成了

原文地址:https://www.cnblogs.com/snowbaby-kang/p/4435600.html