为什么id的值随机化?

时间:2016-12-16 13:45:41

标签: php mysql

我的代码是:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SomuFinance - Personal Finance Manager</title>
    <link rel="stylesheet" type="text/css" href="indexStyle.css">
    <script src="scripts/jquery-3.1.0.min.js"></script>
    <script type="text/javascript" src="scripts/jquery.validate.min.js"></script>
    <style type="text/css">
        #addItemContainer {
            background-color: rgba(204,207,232,1);
        }
    </style>
    <script type="text/javascript">
        var flag=0;
    </script>
</head>
<body>
    <form id="list" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <div id="container">
            <input type="submit" class="button" name="edit" id="edit" value="Edit" />
            <input type="button" class="button" name="delete" value="Delete" />
            <input type="hidden" id="action" name="action">
            <table id="listDB">
                <tr>
                    <th>Select</th>
                    <th>ID</th>
                    <th>Category ID</th>
                    <th>Shop</th>
                    <th>Item</th>
                    <th>Quantity</th>
                    <th>Unit</th>
                    <th>Price Based On</th>
                    <th>MRP</th>
                    <th>Seller's Price</th>
                    <th>Last Updated On</th>
                </tr>
                <?php
                    $dbc =  mysqli_connect('localhost','root','atlantis2016','itemDB')
                                or die("Error Connecting to Database");

                    if(isset($_POST['confirmDelete']))
                    {
                        if($_POST['action']=='confirmDelete')
                        {
                            foreach ($_POST['selected'] as $delete_id) 
                            {
                                $query = "DELETE FROM grocery WHERE id = $delete_id";
                                mysqli_query($dbc, $query)
                                    or die('Error querying database.');
                            }
                        }
                    }

                    $query1 = "SELECT DISTINCT category FROM grocery";
                    $result1 = mysqli_query($dbc, $query1)
                                or die("Error Querying Database");

                    while($row = mysqli_fetch_array($result1))
                    {
                        $category = $row['category'];
                        $query2 = "SELECT * FROM grocery WHERE category='$category' ORDER BY item ASC";
                        $result2 = mysqli_query($dbc, $query2)
                                or die("Error Querying Database");

                        echo '<tr>';
                            echo '<td class="catHead" colspan=11>'.$category.'</td>';
                        echo '</tr>';
                        $catCount=1;

                        while($inRow = mysqli_fetch_array($result2))
                        {
                            $id = $inRow['id'];
                            $shop = $inRow['shop'];
                            $item = $inRow['item'];
                            $qnty = $inRow['quantity'];
                            $unit = $inRow['unit'];
                            $price_based_on = $inRow['price_based_on'];
                            $mrp = $inRow['MRP'];
                            $sellers_price = $inRow['sellers_price'];
                            $last_updated_on = $inRow['last_updated_on'];

                            echo '<tr>';
                                echo '<td><input type="checkbox" id="selected" value="' . $id . '" name="selected[]" /></td>';
                                echo '<td>'.$id.'</td>';
                                echo '<td>'.$catCount.'</td>';
                                echo '<td>'.$shop.'</td>';
                                echo '<td class="leftAligned">'.$item.'</td>';
                                echo '<td>'.$qnty.'</td>';
                                echo '<td>'.$unit.'</td>';
                                echo '<td>'.$price_based_on.'</td>';
                                echo '<td class="pri">₹'.$mrp.'</td>';
                                echo '<td class="pri">₹'.$sellers_price.'</td>';
                                echo '<td>'.$last_updated_on.'</td>';
                            echo '</tr>';

                            $catCount++;
                        }
                    }
                ?>
            </table>
        </div>

        <div class="dialogBG">
            <div id="deleteConfirmDialog" class="dialog">
                <div class="closeDialog"></div>
                <p>Sure you want to delete the selected Data?</p>
                <input type="submit" id="confirmDelete" class="dialogButton" name="confirmDelete" value="Delete" />
                <input type="button" id="cancelDelete" class="dialogButton cancelButton" name="cancelDelete" value="Cancel" />
            </div>
        </div>
    </form>

    <div class="dialogBG">
        <div id="addItemContainer" class="dialog">
            <div class="closeDialog"></div>
            <h1>Edit Item</h1>
            <form id="data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
                <?php
                    if(isset($_POST['action']))
                    {
                        if($_POST['action']=='edit')
                        {
                            echo '<script>flag=1;</script>';
                            foreach ($_POST['selected'] as $edit_id) 
                            {
                                $query = "SELECT * FROM grocery WHERE id = $edit_id";
                                $result = mysqli_query($dbc, $query)
                                            or die('Error querying database.');
                                break;
                            }

                            $inRow = mysqli_fetch_array($result);

                            $id = $inRow['id'];
                            $shop = $inRow['shop'];
                            $category = $inRow['category'];
                            $item = $inRow['item'];
                            $qnty = $inRow['quantity'];
                            $unit = $inRow['unit'];
                            $price_based_on = $inRow['price_based_on'];
                            $mrp = $inRow['MRP'];
                            $sellers_price = $inRow['sellers_price'];
                            $last_updated_on = $inRow['last_updated_on'];

                        }
                    }
                ?>
                <div class="leftAligned">
                    <div class="inp">
                        <label for="shop">ID : </label>
                        <input type="text" id="id" name="id" value="<?php echo $id; ?>" required disabled>
                    </div> <br>
                    <div class="inp">
                        <label for="shop">Shop : </label>
                        <input type="text" id="shop" name="shop" value="<?php echo $shop; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="category">Category : </label>
                        <input type="text" id="category" name="category" value="<?php echo $category; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="item">Item : </label>
                        <input type="text" id="item" name="item" value="<?php echo $item; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="qnty">Quantity : </label>
                        <input type="text" id="qnty" name="qnty" value="<?php echo $qnty; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="unit">Unit : </label>
                        <input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="price_based_on">Price based on : </label>
                        <select name="price_based_on" id="price_based_on">
                            <option value="kilos">Kilos</option>
                            <option value="packet">Packet</option>
                            <option value="bottle">Bottle</option>
                            <option value="box">Box</option>
                            <option value="piece">Piece</option>
                        </select>
                    </div> <br>
                    <div class="inp">
                        <label for="mrp">MRP (₹) : </label>
                        <input type="text" id="mrp" name="mrp" value="<?php echo $mrp; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="sellers_price">Seller's Price (₹) : </label>
                        <input type="text" id="sellers_price" value="<?php echo $sellers_price; ?>" name="sellers_price" required>
                    </div> <br>
                    <div class="inp">
                        <label for="last_updated_on">Last Updated on : </label>
                        <input type="date" id="last_updated_on" name="last_updated_on" value="<?php echo $last_updated_on; ?>" required>
                    </div>
                </div>
                <div class="inp">
                    <input id="insertButton" type="submit" name="submit" value="Insert">
                </div>
                <div id="message">
                    <?php
                        if(isset($_POST['submit']))
                        {

                            echo "<script> alert('$id'); </script>";
                            $shop = $_POST['shop'];
                            $category = $_POST['category'];
                            $item = $_POST['item'];
                            $qnty = $_POST['qnty'];
                            $unit = $_POST['unit'];
                            $price_based_on = $_POST['price_based_on'];
                            $mrp = $_POST['mrp'];
                            $sellers_price = $_POST['sellers_price'];
                            $last_updated_on = $_POST['last_updated_on'];
                            $result=null;

                            $query = "UPDATE grocery SET shop='$shop', category='$category', item='$item', quantity='$qnty', unit='$unit', price_based_on='$price_based_on', mrp='$mrp', sellers_price='$sellers_price', last_updated_on='$last_updated_on' WHERE id='$id'";

                            if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))
                            {
                                $result = mysqli_query($dbc, $query)
                                                    or die(mysqli_error($dbc));
                            }

                            if($result)
                            {
                                echo '<span class="success">Item Edited Successfully!</span>';  
                            }
                            else
                            {
                                echo '<span class="failure">Failed to insert Item.</span>';
                            }
                            //header("Refresh:2");
                        }
                    ?>
                    <script>
                    $(document).ready(function(){

                        $( "#data" ).validate({
                          rules: {
                            qnty: {
                              number: true
                            },
                            mrp: {
                              number: true
                            },
                            sellers_price: {
                              number: true
                            }
                          },
                          messages: {
                            qnty : {
                                number: '<br> <span class="failure err">Enter a valid quantity</span>'
                            },
                            mrp : {
                                number: '<br> <span class="failure err">Enter a valid MRP</span>'
                            },
                            sellers_price : {
                                number: '<br> <span class="failure err">Enter a valid Price</span>'
                            },
                          }
                        });
                    });
                    </script>
                </div>
            </form>
        </div>
    </div>
    <script type="text/javascript">
        $(document).ready(function(){
            $('.button').click(function(event){
                if($(this).val()=="Delete")
                {
                    $("#deleteConfirmDialog").show(200).parent(".dialogBG").fadeIn(200);
                    $("#action").val('confirmDelete');
                }
                else if($(this).val()=="Edit")
                {
                    event.preventDefault();
                    $("#action").val('edit');
                    $("#list").submit();
                }
            });

            if(flag===1)
            {
                console.log("This shouldn't be there if the page reloads!");
                $("#addItemContainer").show(200).parent(".dialogBG").fadeIn(200);
            }

            $('#confirmDelete').click(function(){
                $(".closeDialog").trigger("click");
            });
            $('#cancelDelete').click(function(){
                $("input:checkbox[name='selected[]']").prop('checked', false);
            });
            $(".closeDialog").click(function (e){
                $(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
            });
            $(".cancelButton").click(function (e){
                $(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
            });
        });
    </script>

    <?php
        mysqli_close($dbc);
    ?>
</body>
</html>

请注意,我使用以下行获取第一个选定元素的id:

foreach ($_POST['selected'] as $edit_id) 
{
    $query = "SELECT * FROM grocery WHERE id = $edit_id";
    $result = mysqli_query($dbc, $query)
                or die('Error querying database.');
    break;
}

$inRow = mysqli_fetch_array($result);

$id = $inRow['id'];

现在,我使用这样获得的id(与edit_id完全相同)来访问数据库中的记录,并在弹出窗口中显示它。

<div class="leftAligned">
      <div class="inp">
            <label for="shop">ID : </label>
            <input type="text" id="id" name="id" value="<?php echo $id; ?>" required disabled>
      </div> <br>
      <div class="inp">
            <label for="shop">Shop : </label>
            <input type="text" id="shop" name="shop" value="<?php echo $shop; ?>" required>
      </div> <br>
      <div class="inp">
            <label for="category">Category : </label>
            <input type="text" id="category" name="category" value="<?php echo $category; ?>" required>
      </div> <br>
      <div class="inp">
          <label for="item">Item : </label>
          <input type="text" id="item" name="item" value="<?php echo $item; ?>" required>
      </div> <br>
      <div class="inp">
          <label for="qnty">Quantity : </label>
          <input type="text" id="qnty" name="qnty" value="<?php echo $qnty; ?>" required>
      </div> <br>
      <div class="inp">
          <label for="unit">Unit : </label>
          <input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" required>
      </div> <br>
      <div class="inp">
          <label for="price_based_on">Price based on : </label>
          <select name="price_based_on" id="price_based_on">
              <option value="kilos">Kilos</option>
              <option value="packet">Packet</option>
              <option value="bottle">Bottle</option>
              <option value="box">Box</option>
              <option value="piece">Piece</option>
          </select>
      </div> <br>
      <div class="inp">
          <label for="mrp">MRP (₹) : </label>
          <input type="text" id="mrp" name="mrp" value="<?php echo $mrp; ?>" required>
      </div> <br>
      <div class="inp">
          <label for="sellers_price">Seller's Price (₹) : </label>
          <input type="text" id="sellers_price" value="<?php echo $sellers_price; ?>" name="sellers_price" required>
      </div> <br>
      <div class="inp">
          <label for="last_updated_on">Last Updated on : </label>
          <input type="date" id="last_updated_on" name="last_updated_on" value="<?php echo $last_updated_on; ?>" required>
      </div>
</div>

到目前为止,ID和所有相关细节完全符合它们的要求。

然而,更进一步,当我尝试使用ID更新记录时,使用语句

UPDATE grocery SET shop='$shop', category='$category', item='$item', 
                    quantity='$qnty', unit='$unit', price_based_on='$price_based_on', 
                    mrp='$mrp', sellers_price='$sellers_price', 
                    last_updated_on='$last_updated_on' 
WHERE id='$id'

以某种方式,id已更改为表中id的最后一个值。因此,不是用更新的值更新表的正确记录,因为id&#34;神奇地&#34;此时发生更改(按下sumbit按钮后),将显示id的最后一个值。我希望id与原始id保持一致。

为什么会发生这种情况,我该如何解决?

0 个答案:

没有答案