如何使用PHP / HTML表单更新MySQL数据?

时间:2019-04-02 16:54:10

标签: php mysql

我有一个PHP / HTML表单,该表单应该能够允许用户将项目的状态更改为“活动”或“非活动”。当我提交表单时,会出现我的成功消息,但查询不会执行。文件名为editStatus.php

我尝试更改MySQL查询中的某些语法,但无济于事。

<html>
<head>
<meta charset="utf-8">
<title>Edit Status | Apparel</title>
<link href="inventoryManagerStyles.css" rel="stylesheet">
</head>
<body>
<h1>Edit Status | Apparel</h1>
<table>
    <tr>
        <td class="applyFont">
            <form action="editStatus.php" method="POST">
            STATUS: <input type="text" name="isActive" 
placeholder="Active/Inactive" required><br>
        </td>
    </tr>
    <tr>
        <td>
            <input type="submit" name="update" 
value="Update Status" class="submitBtn">
            </form>
            <a href='viewApparel.php'><button 
class='button'>Back</button></a>
        </td>
    </tr>
</table>
</body>

<?php

if(isset($_POST['update'])) {
    //Connect to DB
    $hostname = "hostname";
    $username = "username";
    $password = "password";
    $dbName = "dbName";

            $con = mysqli_connect($hostname, $username, $password, 
            $dbName);

    //Get Value From User
    $isActive = $_POST['isActive'];
    $ID = $_POST['ID'];

            //Query to Update Data
    $query = "UPDATE `Apparel` SET `isActive`='".$isActive."' 
            WHERE ID='$ID'";
    $result = mysqli_query($con, $query);

    //Check if Query Was Successful
    if($result) {
                echo "Status updated to $isActive";
    } else {;
        echo "Error updating the status of the item.";
    }   
    //Disconnect From DB
    mysqli_close($con);
}
?>

</html>

我很想了解如何获取此查询以将项目状态实际更改为有效/无效。

0 个答案:

没有答案