This question is an exact duplicate of:
I am performing some Crud operations without refreshing page in PHP. All the operations work except for update,it gives me a dialog box showing an error of undefined index id and showing me the final result "Data Updated". But this updation does not remain on screen when the page is refreshed nor it is seen in my database.I get my previous data as it is. Please help me fix this.
Below is the code- This is my main page :
function edit_data(id,text,column_name)
{
$.ajax({
url:"update.php",
method:"POST",
data:{id:id, text:text, column_name:column_name},
dataType:"text",
success:function(data){
alert(data);
}
});
}
I have used blur event for updation.
$(document).on('blur','.name',function(){
var id= $(this).data("id1");
var name=$(this).text();
edit_data(id,name,"name");
});
$(document).on('blur','.lname',function(){
var id= $(this).data("id2");
var lname=$(this).text();
edit_data(id,lname,"lname");
});
select.php
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$output .='
<tr>
<td>'.$row["id"].'</td>
<td class="name" data-id1"'.$row["id"].'" contenteditable="true">'.$row["name"].'</td>
<td class="lname" data-id2"'.$row["id"].'" contenteditable="true">'.$row["lname"].'</td>
<tr/>';
}
update.php I get error for this page
<?php
$link=mysql_connect("localhost","root","");
mysql_select_db("db2017",$link);
$id=$_POST["id"]; //Undefined index "id"
$text=$_POST["text"];
$column_name=$_POST["column_name"];
$sql="update detail set ".$column_name."='".$text."' where id='".$id."' ";
if(mysql_query($sql))
{
echo "Data Updated";
}
?>
Kindly ignore the use of mysql_query instead of mysqli_query. The function is not working on my browser hence switched to mysql-query().
Also I tried if(isset($_POST['name'])){ $name = $_POST['name']; }
according to previous answers on Stackoverflow. Doesn't work!!
</div>