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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值