2016-08-03 109 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