0

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!!

Celin Matthews
  • 61
  • 1
  • 3
  • 11
  • The reason you use `mysqli` over `mysql_` is for the parameterized queries functionality which closes the SQL injection hole. That could be the issue here. – chris85 Feb 22 '17 at 02:23

1 Answers1

0

Data id in your html markup didnt have = assignment operator :

<td class="name" data-id1"'.$row["id"].'" contenteditable="true">'.$row["name"].'</td>
<td class="lname" data-id2"'.$row["id"].'" contenteditable="true">'.$row["lname"].'</td>

Should be :

<td class="name" data-id1="'.$row["id"].'" contenteditable="true">'.$row["name"].'</td>
<td class="lname" data-id2="'.$row["id"].'" contenteditable="true">'.$row["lname"].'</td>

End up, this below code will always got undefined data :

var id= $(this).data("id1"); // undefined
var id= $(this).data("id2"); // undefined

When the data is undefined, the data you send using ajax request will skip id property if the value is undefined :

data:{id:id, text:text, column_name:column_name},

When the request reach the server side endpoint, it will look at the $_POST["id"], but POST data not having an index with the name id, ends up you got the errors undefined index

Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40