删除无法通过提交按钮操作的数据库条目

时间:2019-02-23 01:01:41

标签: php mysql

我有一个用DIVS构造的自制数据网格(表),用于显示与工作相关的零件,我添加了一个隐藏表格以添加数据记录,并添加了一个按钮以从两个表中删除特定记录,但是我无法获取删除查询才能正确执行。

已将删除查询的提交作为一个表单按钮进行了,我还尝试使用输入和js脚本进行操作,然后触发查询,但均不成功,因此尝试使其保持简单。

单击按钮后,页面将刷新,它应在查询末尾(partdel)刷新,但是记录并未删除,我认为查询未接收到$ part_id变量,但是我不确定如何将此传递给它。

由于设计原因,我需要在表单中执行此操作,并且不确定如何进行。

我们将不胜感激,甚至将其指向正确的方向以增加我的学习曲线。

<?php

        ////////////////////////         DELETE FROM PARTS LIST FOR ORDER    //////////////////////////////

        if($_POST['partdel'])
        {
            //Delete part query
            $delQuery = "DELETE FROM job_parts, parts INNER JOIN parts ON parts.part_id = job_parts.part_id WHERE parts.part_id = '$part_id'";  
            $resultDel = mysqli_query($conn, $delQuery);
            $_SESSION['message'] = "Updated!"; 
            header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
            exit;
        }

        ////////////////////////         DISPLAY PARTS DATA RELATIVE TO THE JOB    //////////////////////////////

        if(isset($_GET['id']))  
        {
        $id = $_GET['id'];
        $result = mysqli_query($conn,"SELECT part_id, part_name, part_cost, part_rrp, quantity FROM jobs INNER JOIN job_parts USING (job_id) 
            INNER JOIN parts USING (part_id) Where job_id = '$id'");

        echo "<div class='divTable'>
                <div class='divTableBody'>
                <div class='divTableRow'>
                    <div class='divTableHead'>Part Name</div>
                    <div class='divTableHead'>Qty</div>
                    <div class='divTableHead'>Cost Price</div>
                    <div class='divTableHead'>Cost Total</div>
                    <div class='divTableHead'>Retail Price</div>
                    <div class='divTableHead'>Total</div>
                    <div class='divTableHead'></div>
                                        </div>";
        while($row = mysqli_fetch_array($result))
            {   
            $part_id = $row['0'];  //DEFINE VAR FOR part_id.
            $costTotal = $row['2'] * $row['4'];
            $rrpTotal = $row['3'] * $row['4'];

                echo "<div class='divTableRow'>";
                        //echo $part_id;    //used to determin weather the variable has been passed.
                        echo "<div class='divTableCell'>" . $row['1'] . "</div>";
                        echo "<div class='divTableCell'>". $row['4'] . "</div>";
                        echo "<div class='divTableCell'>&pound;" . number_format($costTotal, 2) . "</div>";
                        echo "<div class='divTableCell'>&pound;" . number_format($row['2'], 2) . "</div>";
                        echo "<div class='divTableCell'>&pound;" . number_format($row['3'], 2) . "</div>";
                        echo "<div class='divTableCell'>&pound;" .number_format($rrpTotal, 2) . "</div>";


                        //submit by POST subpartdel where $part_id is captured and record deleted using query $delQuery.                            
                        echo "<form action='' method='POST'><input type='submit' name='partdel' id='partdel' value='Delete'></form>";
                        //end DELETE.


                    echo "</div>";      
            }   
            echo "</div></div>";
        }else
        {
            echo "<H1><b><em>No data to get!</em></b></H1>";
        }

            ////////////////////////         ADD PARTS TO THE JOB VIA HIDDEN INPUT FORM    //////////////////////////////


        ?>
        <script>
        function openForm() {
          document.getElementById("myForm").style.display = "block";
        }

        function closeForm() {
          document.getElementById("myForm").style.display = "none";
        }
        </script>


        <button class="open-button" onclick="openForm()">Add Part</button>

        <div class="form-popup" id="myForm">
          <form action="" class="form-container" method="POST">
            <h3>Add part</h3>



            <div class="divTable">
                <div class="divTableBody">
                    <div class="divTableRow">
                        <div class="divTableCell"><label for="name"><strong>Name/Description</strong></label></div>
                        <div class="divTableCell"><input id="partName" name="partName" required="" type="text" placeholder="Enter description" /></div>
                    </div>
                    <div class="divTableRow">
                        <div class="divTableCell"><label for="cost"><strong>Cost Price </strong></label></div>
                        <div class="divTableCell"><input id="partCost" name="partCost" required="" type="text" placeholder="Enter cost price" /></div>
                    </div>
                    <div class="divTableRow">
                        <div class="divTableCell"><label for="retail"><strong>Retail Price </strong></label></div>
                        <div class="divTableCell"><input id="partRetail" name="partRetail" required="" type="text" placeholder="Enter retail price" /></div>
                    </div>
                    <div class="divTableRow">
                        <div class="divTableCell"><label for="quantity"><strong>Quantity </strong></label></div>
                        <div class="divTableCell"><input id="partQuantity" name="partQuantity" required="" type="text" placeholder="Enter quantity" /></div>
                    </div>
                    <div class="divTableRow">
                        <div class="divTableCell"><button type="submitpart" name="submitpart" class="btn" onClick="alert('Added!')">Add</button></div>
                        <div class="divTableCell"> <button type="button" class="btn cancel" onclick="closeForm()">Close</button></div>
                    </div>
                </div>
            </div>


          </form>
        </div>

我的数据库结构如下:

DB Structure

2 个答案:

答案 0 :(得分:0)

这可能无法解决代码中的所有问题,但是太长了,无法发布为注释...

这不是有效的MySQL语法:

  

从job_parts中删除零件,将零件INNER JOIN零件删除parts.part_id = job_parts.part_id WHERE parts.part_id ='$ part_id'“

如果您希望一次从两个表中删除,则这是继续进行的方法:

@IBDesignable
class ConcaveView: UIView {
    @IBInspectable var depth: CGFloat = 10         { didSet { updatePath() } }
    @IBInspectable var fillColor: UIColor = .red   { didSet { shapeLayer.fillColor = fillColor.cgColor } }

    private lazy var shapeLayer: CAShapeLayer = {
        let shapeLayer = CAShapeLayer()
        shapeLayer.fillColor = fillColor.cgColor
        shapeLayer.shadowColor = UIColor.black.cgColor
        shapeLayer.shadowRadius = 5
        shapeLayer.shadowOpacity = 1
        shapeLayer.shadowOffset = .zero
        return shapeLayer
    }()

    override init(frame: CGRect = .zero) {
        super.init(frame: frame)
        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configure()
    }

    func configure() {
        layer.addSublayer(shapeLayer)
        clipsToBounds = false
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        updatePath()
    }

    func updatePath() {
        let path = UIBezierPath()
        let point0 = CGPoint(x: bounds.minX, y: bounds.minY)
        let point2 = CGPoint(x: bounds.maxX, y: bounds.minY)
        let point1 = CGPoint(x: bounds.width / 2, y: bounds.minY + depth)
        path.move(to: point0)
        path.addQuadCurve(to: point2, controlPoint: point1)
        path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
        path.addLine(to: CGPoint(x: bounds.minX, y: bounds.maxY))
        path.addLine(to: point0)

        shapeLayer.path = path.cgPath
    }
}

请参见此 Demo on DB Fiddle (您可以取消注释初始查询以查看其产生的错误)。


更好的是,如果您希望使这种行为机械化(即,每当从DELETE jp, p FROM parts p INNER JOIN job_parts jp ON jp.part_id = p.part_id WHERE p.part_id = ? 中删除父记录时,自动删除job_parts中的子行),则应该在其中设置外键引用parts的表job_parts,例如:

parts

然后您可以简单地进行:

ALTER TABLE job_parts 
    ADD FOREIGN KEY (part_id) REFERENCES parts(part_id)
    ON DELETE CASCADE
;

...,请放心,DELETE FROM parts WHERE part_id =? 中的子记录也已删除。

答案 1 :(得分:0)

感谢您的帮助,最简单的解决方案似乎是FK更改,我已经设置了FK,但是设置为限制。

最终使用的查询如下:

if(isset($_GET['del'])) {
            $del = $_GET['del'];
            mysqli_query($conn, "DELETE FROM parts WHERE part_id=$del");
            //$_SESSION['msg'] = "Part deleted";
            header ('location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]');
        }