doubian0284 2017-02-22 02:19
浏览 19
已采纳

更新操作不能在PHP中运行未定义的索引错误[重复]

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>
  • 写回答

1条回答 默认 最新

  • dongzhuo2010 2017-02-22 02:24
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大