用php更新数据(使用html格式)会导致更新所有行的数据

时间:2018-07-28 08:42:38

标签: php html sql-update

我想用表simple_stall编辑名为order_detail的数据库中的数据...当前,我已经完成了一个页面,其中显示了No Name的数据列表Ordered_Item Quantity。当用户单击No时,它们将被重定向到一个新页面,该页面仅显示他单击的数据。

现在,当用户单击Edit按钮时,他们将被重定向到名为update_info.php的新页面。这是一种更改Name Ordered_ItemQuantity的形式...但是现在当我单击update order按钮时,它将所有行更新为用户刚刚放入的数据在... 我想要的是仅更新用户单击的那个No的数据

这是代码 order_detail.php

<?php

include_once 'dbh.php';

$query = "SELECT * FROM order_detail"; //You don't need a ; like you do in SQL
$result = mysqli_query($connection, $query);

echo "<table border = 1px>"; // start a table tag in the HTML

while($row = mysqli_fetch_array($result))
{   
    $no = $row['No'];
    //Creates a loop to loop through results
    echo  "<tr><td style = 'width:30px;'>" . "<a href='view_more.php?no=$no'>" .$row['No'] . "</td>
               <td style = 'width:30%;'>" . $row['Name'] . "</td>
               <td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
               <td>" . $row['Quantity'] . "</td></tr>";  //$row['index'] the index here is a field name
}

echo "</table>"; //Close the table in HTML
echo "<button type='button'><a href='./index.php'>Back</a></button>";

view_more.php

if (isset($_GET['no']))
{
    include_once 'dbh.php';

    $no = $_GET['no'];
    $query = "SELECT * FROM order_detail WHERE No = '$no'";

    $result = mysqli_query($connection, $query);

    echo "<table border = 1px>"; // start a table tag in the HTML

    while($row = mysqli_fetch_array($result))
    {   

        //Creates a loop to loop through results
        echo  "<tr><td style = 'width:30px;'>" . $row['No'] . "</td>
                   <td style = 'width:30%;'>" . $row['Name'] . "</td>
                   <td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
                   <td>" . $row['Quantity'] . "</td></tr>";  //$row['index'] the index here is a field name
    }

    echo "</table>"; //Close the table in HTML

    echo "<button type='button'><a href='./update_info.php?no=$no'>Edit</a></button>";
    echo "<button type='button'><a href='#'>Delete</a></button>";
    echo "<button type='button'><a href='./order_detail.php'>Back</a></button>";

    mysqli_close($connection);

update_info.php

<form action="update_data.php" method="POST">
            <div>
                <input type="text" name="NewName" placeholder="Name">
            </div>
            <div>
                <input type="text" name="NewOrder" placeholder="Order">
            </div>
            <div>
                <input type="text" name="NewQuantity" placeholder="Quantity">
            </div>
            <div>
                <button type="submit" name="submit">Update Order</button>
            </div>
        </form>

update_data.php

if(isset($_POST['submit']))
{
    include_once 'dbh.php';

    $update = "UPDATE order_detail SET Name='$_POST[NewName]', Ordered_Item='$_POST[NewOrder]', Quantity='$_POST[NewQuantity]' ";

    if (mysqli_query($connection, $update))
    {
        header("Location: ./order_detail.php");
        exit();
    }
    else
    {
        header("Location: ./order_detail.php?update=failed");
        exit();
    }
}

3 个答案:

答案 0 :(得分:0)

添加Where子句并传递您要编辑其值的id。请看下面的代码

 if(isset($_POST['submit']))
    {
        include_once 'dbh.php';

        $update = "UPDATE order_detail SET Name='$_POST[NewName]', Ordered_Item='$_POST[NewOrder]', Quantity='$_POST[NewQuantity]' WHERE No='$_GET[no]' ";

        if (mysqli_query($connection, $update))
        {
            header("Location: ./order_detail.php");
            exit();
        }
        else
        {
            header("Location: ./order_detail.php?update=failed");
            exit();
        }
    }

答案 1 :(得分:0)

指定要更新的订单

看看我有更改的HTML和SQL

<form action="update_data.php" method="POST">
            <div>
                <input type="text" name="NewName" placeholder="Name">
            </div>
            <div>
                <input type="text" name="NewOrder" placeholder="Order">
            </div>
            <div>
                <input type="text" name="NewQuantity" placeholder="Quantity">
            </div>
            <div>





                <input type="hidden" name="No">
                <!-- Specify which order you want to update  -->




                <button type="submit" name="submit">Update Order</button>
            </div>
        </form>

update_data.php

if(isset($_POST['submit']))
{
    include_once 'dbh.php';


    $update = "UPDATE order_detail SET 
            Name='$_POST[NewName]', 
            Ordered_Item='$_POST[NewOrder]', 
            Quantity='$_POST[NewQuantity]' 
     WHERE No = {$_POST['No']} ";
   // add where clause in sql to specify which you want to update


    if (mysqli_query($connection, $update))
    {
        header("Location: ./order_detail.php");
        exit();
    }
    else
    {
        header("Location: ./order_detail.php?update=failed");
        exit();
    }
}

使用准备好的语句,更安全

答案 2 :(得分:0)

像这样向您的update_info.php添加值为$ no的隐藏输入

<form action="update_data.php" method="POST">
    <input type="hidden" name="no" value="<?php $_GET['no']; ?>" />
        <div>
            <input type="text" name="NewName" placeholder="Name">
        </div>
        <div>
            <input type="text" name="NewOrder" placeholder="Order">
        </div>
        <div>
            <input type="text" name="NewQuantity" placeholder="Quantity">
        </div>
        <div>
            <button type="submit" name="submit">Update Order</button>
        </div>
    </form>

,然后在update_data.php中将您的sql查询更改为

"UPDATE order_detail SET Name='$_POST[NewName]', Ordered_Item='$_POST[NewOrder]', Quantity='$_POST[NewQuantity]' where = {$_POST['no']} ";