duandu8707 2014-03-09 17:09
浏览 41
已采纳

PHP MySQLi编写的语句正在删除错误的行

I'm working on a script, where one of the features should be to delete a row in the table, however when I try to delete the row it ends up deleting the WRONG row.

The code in my script is as follows:

<?php

session_start();

include '../includes/conn.php';

$verified = $_SESSION['verified'];

if($verified !== true){
    header("Location: index.php");
}

if(isset($_POST['id'])){

    $query = $conn->prepare("DELETE FROM inventory WHERE ID=?");
    $query->bind_param('s', $_POST['id']);
    $query->execute();

}

if(isset($_POST['desc']) && isset($_POST['mcost']) && isset($_POST['price']) && isset($_POST['rrp']) && isset($_POST['loc'])){

    date_default_timezone_set("Europe/London");
    $date = (string)date("l jS \of F Y");

    $query = $conn->prepare("INSERT INTO inventory (Description, MaterialsPrice, Price, RRP, Location, Date_Created) VALUES (?, ?, ?, ?, ?, ?)");
    $query->bind_param('ssssss', $_POST['desc'], $_POST['mcost'], $_POST['price'], $_POST['rrp'], $_POST['loc'], $date);
    $query->execute();

    header("Location: inventory.php");

}

?>

<html>

    <head>

        <title>Encanto Jewellery - Inventory</title>

        <style type="text/css">

            @import url(../includes/tablestyles.css);

        </style>

    </head>

    <body>

        <?php

            $desca = array();
            $mpa = array();
            $pa = array();
            $rrpa = array();
            $loca = array();
            $datea = array();

            $query = $conn->prepare("SELECT Description, MaterialsPrice, Price, RRP, Location, Date_Created FROM inventory ORDER BY ID desc");
            $query->execute();
            $query->bind_result($desc, $mp, $p, $rrp, $loc, $date);

            while($query->fetch())
            {
                $desca[] = $desc;
                $mpa[] = $mp;
                $pa[] = $p;
                $rrpa[] = $rrp;
                $loca[] = $loc;
                $datea[] = $date;
            }

        ?>

        <form action="" method="POST" name="insert">

            <div class="col" style="border-style: solid solid none solid;">

                <div class="row">

                    <b><p>Description</p></b>

                </div>

                <?php

                    for($i = 0; $i < count($desca);  $i++)
                    {
                        echo '<div class="row"><p>' . $desca[$i] . '</p></div>';
                    }

                ?>

                <div class="row">

                    <textarea style="width: 99%;" rows="1" name="desc"></textarea>

                </div>

            </div>

            <div class="col">

                <div class="row">

                    <b><p>Price of Materials</p></b>

                </div>

                <?php

                    for($i = 0; $i < count($mpa);  $i++)
                    {
                        echo '<div class="row"><p>' . $mpa[$i] . '</p></div>';
                    }

                ?>

                <div class="row">

                    <textarea style="width: 99%;" rows="1" name="mcost"></textarea>

                </div>

            </div>

            <div class="col">

                <div class="row">

                    <b><p>Price</p></b>

                </div>

                <?php

                    for($i = 0; $i < count($pa);  $i++)
                    {
                        echo '<div class="row"><p>' . $pa[$i] . '</p></div>';
                    }

                ?>

                <div class="row">

                    <textarea style="width: 99%;" rows="1" name="price"></textarea>

                </div>

            </div>

            <div class="col">

                <div class="row">

                    <b><p>RRP</p></b>

                </div>

                <?php

                    for($i = 0; $i < count($rrpa);  $i++)
                    {
                        echo '<div class="row"><p>' . $rrpa[$i] . '</p></div>';
                    }

                ?>

                <div class="row">

                    <textarea style="width: 99%;" rows="1" name="rrp"></textarea>

                </div>

            </div>

            <div class="col">

                <div class="row">

                    <b><p>Location</p></b>

                </div>

                <?php

                    for($i = 0; $i < count($loca);  $i++)
                    {
                        echo '<div class="row"><p>' . $loca[$i] . '</p></div>';
                    }

                ?>

                <div class="row">

                    <textarea style="width: 99%;" rows="1" name="loc"></textarea>

                </div>

            </div>

            <div class="col">

                <div class="row">

                    <b><p>Date Created</p></b>

                </div>


                <?php

                    for($i = 0; $i < count($datea);  $i++)
                    {
                        echo '<div class="row"><p>' . $datea[$i] . '</p></div>';
                    }

                ?>

                <div class="row">
                    <input type="submit" value="Insert" style="height: 24px;" />
                </div>

            </div>

        </form>

        <div class="col">

            <div class="row">

                <b><p>Delete</p></b>

            </div>

            <?php
                for($i = 0; $i < count($datea);  $i++)
                {
            ?>

            <div class="row">

                <form action="" method="POST">

                    <input type="hidden" name="id" value="<?php echo $i + 1; ?>" />
                    <input type="submit" value="Delete" />

                </form>

            </div>

            <?php } ?>

        </div>

    </body>

</html>

Thanks in advance, looking forward to your answers,

  • 写回答

3条回答 默认 最新

  • douvcpx6526 2014-03-09 17:18
    关注

    Is ID an integer? If so, use

    $query->bind_param('i', $_POST['id']);
    

    Also, I suspect you're still be deleting the wrong element. Try these changes:

    1. ADD ID to the query and bind a variable to it:

         $ids = array();
         // other stuff...
      
      $query = $conn->prepare("SELECT ID, Description, MaterialsPrice, Price, RRP, Location,   Date_Created FROM inventory ORDER BY ID desc");
          $query->execute();
          $query->bind_result($id, $desc, $mp, $p, $rrp, $loc, $date);
      
          while($query->fetch())
          {
              $ids[] = $id;
              $desca[] = $desc;
              $mpa[] = $mp;
              $pa[] = $p;
              $rrpa[] = $rrp;
              $loca[] = $loc;
              $datea[] = $date;
          }
      
    2. Then when you generate your delete elemnts, iterate over the $ids array:

       <?php
              foreach($ids as $delete_id){
      
          ?>
      
          <div class="row">
      
              <form action="" method="POST">
      
                  <input type="hidden" name="id" value="<?php echo $delete_id; ?>" />
                  <input type="submit" value="Delete" />
      
              </form>
      
          </div>
      
          <?php } ?>
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?