douqiao1887 2015-07-07 06:23
浏览 17

用PHP更新到mysql

if(isset($_POST['submit'])) {
    require_once('db.php');

    $prod_id = $_POST['product_id'];
    $pname = $_POST['prod_name'];
    $price = $_POST['prod_price'];
    $stock = $_POST['stocks'];
    $category = $_POST['category'];
    $update = null;

    if (($pname = '')){
        $update .= 'product_name = "'.$pname.'"'; 
    }

    if (($pcode = '')){
        if(!($update == '' || $update == null)) $update .= ',';
        $update .= "product_price='".$price."'";
    }

    if (($stock = '')){
        if(!($update == '' || $update == null)) $update .= ',';
        $update .= "stocks='".$stock."'";
    }

    if (($category = '')){
        if(!($update == '' || $update == null)) $update .= ',';
        $update .= "category='".$category."'";
    }

    $sql = 'UPDATE products_table set '.$update.' WHERE product_id = '.(int)$prod_id;
    echo $sql;
    $qry = mysql_query($sql);

    if (!$qry) {
        ?>
        <script>
            alert('Failed to update.');
            window.location.href = 'update.php?prod_id='+<?php echo $prod_id;?>;
        </script>
        <?php
    } else {
        ?>
        <script>
            alert('Product Updated.');
            window.location.href = 'products_list.php';
        </script>
        <?php
    }


}

Here is the update_process.php file.

    <div class="row">
            <h1 class="page-header">Update Product </h1>
    </div>

    <div class="row">
        <label><a href="products_list.php">Return from Products List</a></label>
    </div>

    <div class="row">
        <form method="POST" action="update_process.php" class="input-group">

            <input type="hidden" name="product_id" value="<?php echo $_GET['prod_id']?>" >
            <label>Product Name</label> 
                <input type="text" class="form-control" value="" name="prod_name" /><br />

            <label>Product Price</label>
                <input type="text"  class="form-control" value=""  name="prod_price" /><br />

            <label>Stocks</label> 
                <input type="text"  class="form-control" value="" name="stocks" /><br />
            <label>Category </label> 
                <input type="text"  class="form-control" value= "" name = "category" /> <br/> 


            <input type="submit" class="btn btn-primary" name="submit" value="Update Product" /> &nbsp
            <input type="reset" class="btn btn-primary" value="Clear Fields" />
        </form>
    </div>

<div/> 

This is the update.php file.

I am having a problem on updating a row in my products list, i don't know what seems to be the wrong why it is not updating

  • 写回答

2条回答 默认 最新

  • douxian8883 2015-07-07 06:27
    关注

    conditions are wrong.

    if(($pname = ''))
    

    should be

    if($pname != '')
    

    like that modify for all.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题