JavaScript错误和数据库更新

时间:2017-08-22 20:17:19

标签: javascript php mysql

我的数据库出现问题,并对其进行更新。我有一个包含5列的数据库表库存(stock_id,p_id,brand_id,cat_id,可用性)。我想从前端做一个更新。因此,当弹出窗口显示并填写表单时,UPDATE不起作用。我有3个文件。第一个stock.php读取数据库并正常工作。如果单击“编辑”,则打开的seconf将如下所示:

<?php

session_start();
include ( 'config.php' );
require_once( 'class.db.php' );

$database = DB::getInstance();

if($_POST['rowid']) {
    $id = $_POST['rowid']; //escape string

    $query = "SELECT * FROM stock WHERE stock_id = $id";
    $results = $database->get_results( $query );
    foreach( $results as $row ){
        $cat_id = $row['cat_id'];
        $brand_id = $row['brand_id'];
        $p_id = $row['p_id'];
        ?>

        <form method="post" name="form">
            <input id="stock_id" name="stock_id" type="hidden" value="<?php echo $row['stock_id'];?>"/>

            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <label class="control-label">CATEGORY</label>
                    <select id="category" name="category" class="form-control">
                        <?php
                        $qex = "SELECT * FROM category";
                        $rex = $database->get_results( $qex );
                        foreach( $rex as $rowex ) {
                            ?>

                            <option value="<?php echo $rowex['cat_id']; ?>"<?php
                            if ($cat_id == $rowex['cat_id'])
                                echo 'selected'; ?>><?php echo $rowex['cat_name'];?></option>
                            <?php
                        }
                        ?>
                    </select>
                </div>
            </div>

            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <label class="control-label">BRAND</label>
                    <select id="brand" name="brand" class="switchable form-control">
                        <?php
                        $qex = "SELECT * FROM brand";
                        $rex = $database->get_results( $qex );
                        foreach( $rex as $rowex ) {
                            ?>
                            <option value="<?php echo $rowex['brand_id']; ?>"<?php
                            if ($brand_id == $rowex['brand_id'])
                                echo 'selected'; ?> class="brand_<?php echo $rowex['cat_id'];?>"><?php echo $rowex['brand_name'];?></option>
                            <?php
                        }
                        ?>
                    </select>
                </div>
            </div>

            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <label class="control-label">PRODUCT NAME</label>
                    <select id="product" name="product" class="switchable form-control">
                        <?php
                        $qex = "SELECT * FROM product";
                        $rex = $database->get_results( $qex );
                        foreach( $rex as $rowex ) {
                            ?>
                            <option value="<?php echo $rowex['product_id']; ?>"<?php
                            if ($product_id == $rowex['product_id'])
                                echo 'selected'; ?> class="product_<?php echo $rowex['brand_id'];?>"><?php echo $rowex['product_name'];?></option>
                            <?php
                        }
                        ?>
                    </select>
                </div>
            </div>

            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <label class="control-label">IN STOCK</label>
                    <input type="number" id="availability" name="availability" value="<?php echo $row['availability'];?>" class="form-control"/>
                </div>
            </div>

            <div class="clearfix"></div>
            <div>
                <input type="submit" value="Update Data" class="pull-right btn btn-primary submit" style="margin-right:15px;"/>
                <span class="pull-left error" style="display:none;margin-left:15px;"> Please Enter Valid Data</span>
                <span class="pull-left success" style="display:none;margin-left:15px;"> Data updated!</span>
                <div class="clearfix"></div>

            </div>
        </form>
        <?php
    }
    ?>

    <script type="text/javascript" >
        $(document).ready(function(){

            $(function() {
                $(".submit").click(function() {
                    var stock_id = $("#stock_id").val();
                    var category = $('select[name="category"]').val()
                    var brand = $('select[name="brand"]').val()
                    var product = $('select[name="product"]').val()
                    var availability = $("#availability").val();

                    var dataString =
                            'stock_id='+ stock_id +
                            '&brand=' + brand +
                            '&category=' + category +
                            '&product=' + product +
                            '&availability=' + availability
                        ;

                    if(
                        stock_id=='' ||
                        brand=='' ||
                        category=='' ||
                        product=='' ||
                        availability==''
                    ){
                        $('.success').fadeOut(200).hide();
                        $('.error').fadeOut(200).show();
                    }
                    else
                    {
                        $.ajax({
                            type: "POST",
                            url: "update-stock.php",
                            data: dataString,
                            success: function(){
                                $('.success').fadeIn(200).show();
                                $('.error').fadeOut(200).hide();
                            }
                        });
                    }
                    return false;
                });
            });


            $("#category").change(function () {
                if ($(this).data('options') == undefined) {
                    $(this).data('options', $('select.switchable option').clone());
                }
                var id = $(this).val();
                var that = this;
                $("select.switchable").each(function () {
                    var thisname = $(this).attr('name');
                    var theseoptions = $(that).data('options').filter('.' + thisname + '_' + id);
                    $(this).html(theseoptions);
                });
            });
            //then fire it off once to display the correct elements
            $('#category').trigger('change');

        });/** Document Ready Functions END **/
    </script>

<?php  } ?>

这是我应该更新数据库的update-stock.php的代码:

<?php

session_start();
include ( 'config.php' );
require_once( 'class.db.php' );

$database = DB::getInstance();

if($_POST) {
    $stock_id = $_POST['stock_id'];
    $brand = $_POST['brand'];
    $category = $_POST['category'];
    $product = $_POST['product'];
    $availability = $_POST['availability'];


    $update = array(
        'p_id' => $product,
        'brand_id' => $brand,
        'cat_id' => $cat,
        'availability' => $availability
    );

    $where_clause = array(
        'stock_id' => $stock_id
    );

    $updated = $database->update( 'stock', $update, $where_clause, 1 );
}
?>

我有两个问题。

  1. 更新根本不起作用。我刚收到消息请输入有效数据
  2. 我的表单未显示PRODUCT NAME的正确值。示例:在数据库和我的主表中,从数据库产品名称读取信息是2.1.1但是当我单击编辑并打开显示2.1.3的弹出窗体时,例如。
  3. 非常感谢您的帮助。

    这个工作完成fetch_brand.php:

        <?php
    
    session_start();
    include ( 'config.php' );
    require_once( 'class.db.php' );
    
    $database = DB::getInstance();
    
    if($_POST['rowid']) {
        $id = $_POST['rowid']; //escape string
    
        $query = "SELECT * FROM brand WHERE brand_id = $id";
        $results = $database->get_results( $query );
        foreach( $results as $row ){
          $cat_id = $row['cat_id'];
          ?>
    
    
    
    <form method="post" name="form">
      <input id="brand_id" name="brand_id" type="hidden" value="<?php echo $row['brand_id'];?>"/>
    
      <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
                <label class="control-label">CATEGORY</label>
                <select id="category" name="category" class="form-control">
                <?php
                $qex = "SELECT * FROM category";
                $rex = $database->get_results( $qex );
                foreach( $rex as $rowex ) {
                ?>
                <option value="<?php echo $rowex['cat_id']; ?>"<?php
                if ($cat_id == $rowex['cat_id'])
                echo 'selected'; ?>><?php echo $rowex['cat_name'];?></option>
                <?php
                }
                ?>
                </select>
              </div>
              </div>
    
      <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
          <label class="control-label">BRAND NAME</label>
          <input type="text" id="brand_name" name="brand_name" value="<?php echo $row['brand_name'];?>" class="form-control"/>
        </div>
      </div>
    
    
    
    <div class="clearfix"></div>
    <div>
    <input type="submit" value="Update Data" class="pull-right btn btn-primary submit" style="margin-right:15px;"/>
    <span class="pull-left error" style="display:none;margin-left:15px;"> Please Enter Valid Data</span>
    <span class="pull-left success" style="display:none;margin-left:15px;"> Data updated!</span>
    <div class="clearfix"></div>
    
    </div>
    </form>
    <?php
    }
    ?>
    
    <script type="text/javascript" >
    $(document).ready(function(){
    
    $(function() {
    $(".submit").click(function() {
    var brand_id = $("#brand_id").val();
    var brand_name = $("#brand_name").val();
    var category = $('select[name="category"]').val()
    
    
    var dataString =
    'brand_id='+ brand_id +
    '&brand_name=' + brand_name +
    '&category=' + category
    ;
    
    if(
      brand_id=='' ||
      brand_name=='' ||
      category==''
    ){
    $('.success').fadeOut(200).hide();
    $('.error').fadeOut(200).show();
    }
    else
    {
    $.ajax({
    type: "POST",
    url: "update-brand.php",
    data: dataString,
    success: function(){
    $('.success').fadeIn(200).show();
    $('.error').fadeOut(200).hide();
    }
    });
    }
    return false;
    });
    });
    
    
    
    
      });/** Document Ready Functions END **/
      </script>
    
    <?php  } ?>
    

    update-product.php:

    <?php
    
    session_start();
    include ( 'config.php' );
    require_once( 'class.db.php' );
    
    $database = DB::getInstance();
    
    if($_POST) {
      $p_id = $_POST['p_id'];
      $brand = $_POST['brand'];
      $category = $_POST['category'];
      $product = $_POST['product'];
    
    
      $update = array(
        'product_name' => $product,
        'brand_id' => $brand,
        'cat_id' => $cat
      );
    
      $where_clause = array(
        'p_id' => $p_id
      );
    
      $updated = $database->update( 'product', $update, $where_clause, 1 );
      }
    ?>
    

    这个很棒!所以我很确定我在代码中犯了错误。

0 个答案:

没有答案