简单更新/编辑不适用于PHP / MySql的数据

时间:2016-08-03 05:21:52

标签: php mysql

我正在尝试在数据库中对数据进行简单的编辑/更新。但不知怎的,它不起作用。

所以我能够将保存的数据读出来。我也没有任何错误

enter image description here

我已经盯着我的代码并用Google搜索了几个小时,但我看不出我的代码可能会出错。

印刷的回声给出了以下输出,似乎是正确的:

enter image description here

HTML code:

<form id="formAddCategory" class="FrmCat" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <div class="form-group">
        <!-- hidden id from tbl -->
        <input type="hidden" name="hiddenId" value="<?php echo $hiddenID ?>" />
        <label for="recipient-name" class="control-label">Category Name:</label>
        <input type="text" class="form-control" id="recipient-name1" name="category" required="" value="<?php echo $category ?>" />
     </div>
     <button type="submit" id="btnEditCat" class="btn btn-danger" name="editCategory">Save Category</button>
</form>

我要编辑/更新的部分PHP代码:

<?php
//edit/update data to db
if(isset($_POST['editCategory'])){
  $categoryUpdate = mysqli_real_escape_string($con, $_POST['category']);
  $categoryID = mysqli_real_escape_string($con, $_POST['hiddenId']);
  $qry = "UPDATE tbl_Category SET category = $categoryUpdate WHERE category_id = $categoryID"; 
  $result = mysqli_query($con, $qry);
  echo $qry;

  if($result){
    header("Location: category.php"); 
  }
}

?>

2 个答案:

答案 0 :(得分:2)

您需要单引号'来包装您的参数:

$qry = "UPDATE tbl_Category SET category = '$categoryUpdate' WHERE category_id = '$categoryID'";

答案 1 :(得分:1)

您应该使用单引号(')作为值

 $qry = "UPDATE tbl_Category SET category = '$categoryUpdate' WHERE category_id = '$categoryID'"; 

此外,您可以像这样使用以避免SQL注入(See here

$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?');
$stmt->bind_param('s', $name);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // do something with $row
}