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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?