I have created a table where I can edit inside td
and it works very well. I edited the name of product and it changed successfully just on my webpage, BUT it is not updating into Database.
My database would be like this:
Here is the code for editing inside td
.
$results = $mysqli->query("SELECT * FROM productList");
while($row = $results->fetch_assoc()) {
<td> <span class="edit" id="'.$row['ID'].'"> '.$row['name'].' </span> </td>
}
For editable, I have to use AJAX to call the value back. Something like this:
jQuery(document).ready(function() {
$.fn.editable.defaults.mode = 'popup';
$('.edit').editable();
$(document).on('click','.editable-submit',function(){
var x = $(this).closest('td').children('span').attr('id');
var y = $('.input-sm').val();
var z = $(this).closest('td').children('span');
$.ajax({
url: "edit.php?id="+x+"&data="+y,
type: 'GET',
success: function(s){
if(s == 'status'){
$(z).html(y);}
if(s == 'error') {
alert('Error Processing your Request!');}
},
error: function(e){
alert('Error Processing your Request!!');
}
});
});
});
After AJAX, I have to call url to edit.php
. The code for edit.php
is below:
$mysqli = new mysqli('localhost','root','root','database_name');
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
if($_GET['id'] and $_GET['name'])
{
$id = $_GET['id'];
$name = $_GET['name'];
if ($mysqli->query("UPDATE productList SET name='$name' WHERE ID='$id'"));
$mysqli->close();
echo 'success';
}
I have tried to update manually in phpmyadmin, it works very well there. But not here. It should be working right now. Am I missing something?