Woonghee Cho Woonghee Cho - 1 month ago 6
PHP Question

php mysql update query gives an empty result

I am trying to make update function for my project.

I have a table of Client data,
Delete, Add functions are working but whenever I use update function
it empties data of the row within database to null
I tried other things but didn't work well.
It could be a small mistake but I seem cannot find the problem as I been working on other projects wholeday :<

Update details php file



<?php
// include Database connection file
include("db_connection.php");

// check request
if(isset($_POST))
{
// get values
$id = $_POST['id'];
$Surname = $_POST['update_Surname'];
$Name= $_POST['update_Name'];
$Address = $_POST['update_Address'];
$Telephone = $_POST['update_Telephone'];
$PurchaseDate = $_POST['update_PurchaseDate'];
$Model = $_POST['update_Model'];
$SerialNumber = $_POST['update_SerialNumber'];
$Notes = $_POST['update_Notes'];


// Update User details
$query = "UPDATE Clients SET Surname = '$Surname', Name = '$Name', Address = '$Address', Telephone = '$Telephone', PurchaseDate = '$PurchaseDate', Model = '$Model', SerialNumber = '$SerialNumber', Notes = '$Notes' WHERE id = '$id'";
if (!$result = mysql_query($query)) {
exit(mysql_error());
}
}





update function



function UpdateUserDetails() {
// get values
var update_Surname = $("#update_Surname").val();
var update_Name = $("#update_Name").val();
var update_Address = $("#update_Address").val();
var update_Telephone = $("#update_Telephone").val();
var update_PurchaseDate = $("#update_PurchaseDate").val();
var update_Model = $("#update_Model").val();
var update_SerialNumber = $("#update_SerialNumber").val();
var update_Notes = $("#update_Notes").val();

// get hidden field value
var id = $("#hidden_user_id").val();

// Update the details by requesting to the server using ajax
$.post("ajax/updateUserDetails.php", {
id: id,
Surname: update_Surname,
Name: update_Name,
Address: update_Address,
Telephone: update_Telephone,
PurchaseDate: update_PurchaseDate,
Model: update_Model,
SerialNumber: update_SerialNumber,
Notes: update_Notes
},
function (data, status) {
// hide modal popup
$("#update_user_modal").modal("hide");
// reload Users by using readRecords();
readRecords();
}
);
}





Modal form



<div class="modal fade" id="update_user_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Update</h4>
</div>
<div class="modal-body">

<div class="form-group">
<label for="update_Surname">Surname</label>
<input type="text" id="update_Surname" placeholder="Surname" class="form-control"/>
</div>

<div class="form-group">
<label for="update_Name">Name</label>
<input type="text" id="update_Name" placeholder="Name" class="form-control"/>
</div>

<div class="form-group">
<label for="update_Address">Address</label>
<input type="text" id="update_Address" placeholder="Address" class="form-control"/>
</div>

<div class="form-group">
<label for="update_Telephone">Telephone</label>
<input type="text" id="update_Telephone" placeholder="Telephone" class="form-control"/>
</div>

<div class="form-group">
<label for="update_PurchaseDate">Purchase Date</label>
<input type="date" id="update_PurchaseDate" placeholder="Purchase Date" class="form-control"/>
</div>
<div class="form-group">
<label for="update_Model">Model</label>
<input type="text" id="update_Model" placeholder="Model" class="form-control"/>

</div>
<div class="form-group">
<label for="update_SerialNumber">Serial Number</label>
<input type="text" id="update_SerialNumber" placeholder="Serial Number" class="form-control"/>
</div>
<div class="form-group">
<label for="update_Notes">Notes</label>
<input type="text" id="update_Notes" placeholder="Notes" class="form-control"/>
</div>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" onclick="UpdateUserDetails()" >Save Changes</button>
<input type="hidden" id="hidden_user_id">
</div>
</div>
</div>
</div>




Answer
$id = $_POST['id'];
$Surname = $_POST['update_Surname'];
$Name= $_POST['update_Name'];
$Address = $_POST['update_Address'];
$Telephone = $_POST['update_Telephone'];
$PurchaseDate = $_POST['update_PurchaseDate'];
$Model = $_POST['update_Model'];
$SerialNumber = $_POST['update_SerialNumber'];
$Notes = $_POST['update_Notes'];

you are using update_ as your post variables. But the actual keys are different which you have provided into the jQuery post function. actual keys are defined in following object at left side of each assignment.

     {
        id: id,

        Surname: update_Surname,

        Name: update_Name,

        Address: update_Address,

        Telephone: update_Telephone,

        PurchaseDate: update_PurchaseDate,

        Model: update_Model,

        SerialNumber: update_SerialNumber,

        Notes: update_Notes

    }

use the following instead.

$id = $_POST['id'];
$Surname = $_POST['Surname'];
$Name= $_POST['Name'];
$Address = $_POST['Address'];
$Telephone = $_POST['Telephone'];
$PurchaseDate = $_POST['PurchaseDate'];
$Model = $_POST['Model'];
$SerialNumber = $_POST['SerialNumber'];
$Notes = $_POST['Notes'];
Comments