PHP数据库编辑,值在提交时不会改变

时间:2017-12-10 17:49:41

标签: php html mysqli

我试图添加一个'编辑'我的数据库表输出上的按钮,它从前一个屏幕正确捕获信息,但是当我按下“更新”按钮时按钮,值只是恢复为它们所带来的内容,并且数据库上没有任何更新。有谁知道可能是什么问题?

<?php
session_start();
error_reporting(E_ALL);
include('connect.php');

if ($_SESSION['role']!='Admin') {
    header('Location: index.php');
    exit;
}

if(isset($_GET['edit_id'])) {
    $sql = "SELECT * FROM products WHERE product_id =".$_GET['edit_id'];
    $result = mysqli_query($connection, $sql);
    $row = mysqli_fetch_array($result);

}

if(isset($_POST['btn-update'])){
    $productID = $_POST['productID'];
    $productName = $_POST['productName'];
    $productDesc = $_POST['productDesc'];
    $productType = $_POST['productType'];
    $productPrice = $_POST['productPrice'];
    $stockAmount = $_POST['stockAmount'];
    $update = "UPDATE products SET product_id='$productID', name='$productName', description='$productDesc', type='$productType', price='$productPrice', stock_amount='$stockAmount' WHERE product_id=". $_GET['edit_id'];
    $up = mysqli_query($connection, $update);
    if(!isset($sql)){
        die("$sql Error" .mysqli_connect_error());
    } else {
        header("location: manage-products.php");
    }
} ?>

<!DOCTYPE html>
<html>

<head>
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>

<body>

<form method="post">
    <h1>Edit Product Information:</h1>
    Product ID:<br>
    <input type="number" name="productID" value="<?php echo $row['product_id']; ?>"><br>
    Product Name:<br>
    <input type="text" name="productName" value="<?php echo $row['name']; ?>"><br>
    Product Description:<br>
    <input type="text" name="productDesc" value="<?php echo $row['description']; ?>"><br>
    Product Type:<br>
    <input type="radio" name="productType" value="book" checked> Book<br>
    <input type="radio" name="productType" value="stationary"> Stationary<br>
    <input type="radio" name="productType" value="gift"> Gift<br>
    Product Price:<br>
    <input type="number" min="1" step="any" name="productPrice" value="<?php echo $row['price']; ?>"><br>
    Stock Amount:<br>
    <input type="number" name="stockAmount" value="<?php echo $row['stock_amount']; ?>"><br><br>
    <button type="submit" name=btn-update" id="btn-update" <strong>Update</strong></button>
    <a href="manage-products.php"><button type="button" value="button">Cancel</button></a>
</form>

</body>
</html>

0 个答案:

没有答案