2016-08-03 41 views
1

我想对数据库中的数据进行简单的编辑/更新。但不知何故,它不会工作。简单的更新/编辑数据不适用于PHP/MySql

因此,我可以将保存的数据读出到窗体中。我也没有任何错误

enter image description here

我在我的代码凝视着一派几个小时,但我没有看到我,很可能使我的代码错误。

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

enter image description here

HTML代码:

我的PHP代码
<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 
//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"); 
    } 
} 

?> 

回答

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 
} 
2

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

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

OMG这样的傻事错误和我一直盯着我的代码几个小时。谢谢 – GY22

相关问题