简体   繁体   English

使用AJAX和PHP更新MySQL

[英]Update MySQL using AJAX and PHP

I'll go slow, not for your sake..for mine. 我会慢慢走,不是为了你。是我的。 I'm real new at trying to to do this. 我真的很想这样做。 What I'm trying to do is update live a MySQL DB from an HTML table. 我正在尝试从HTML表实时更新MySQL数据库。 This is how each is built. 这就是每个构建的方式。

 echo ("<td id=\"callsign:$row[recordID]\" contenteditable=\"true\" 
                onClick=\"showEdit(this)\"
                onBlur=\"saveToDatabase(this,'callsign',$row[recordID])\" 
                style='text-transform:uppercase'>
                $row[callsign]</td>");

This is how it renders. 这就是它的渲染方式。

 <td id="callsign:6" contenteditable="true" onclick="showEdit(this)" onblur="saveToDatabase(this,'callsign',6)" style="text-transform: uppercase; background-color: rgb(253, 253, 253); background-position: initial initial; background-repeat: initial initial;">
                KA0SXY</td>

Here is the function that gets called. 这是被调用的函数。

 function saveToDatabase(editableObj,column,id) {

        $(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat right");
            $.ajax({
                url: "saveedit.php",
                type: "POST",
                data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
                success: function(data){
                    $(editableObj).css("background","#FDFDFD");
                }  
            });
      }

And here is the PHP. 这是PHP。

 <?php
require_once "dbConnectDtls.php";

    $result = mysql_query("UPDATE NetLog set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE  recordID=".$_POST["id"]);
    echo $result;

 ?>

When I tab from this cell to the next the only thing that gets executed is the .gif. 当我从此单元格跳至下一个时,唯一会执行的是.gif。 The MySQL does not get updated, what am I doing wrong? MySQL无法更新,我在做什么错?

Thanks in advance for not treating me like a dummy, but helping me learn. 在此先感谢您不把我当做虚拟人,而是帮助我学习。

Followup to suggestions: 后续建议:

I'm afraid I'm making no headway on this. 恐怕我在这方面没有进展。 Is anyone willing to actually write a working example that I can follow? 是否有人愿意写一个我可以效仿的可行示例?

For a newcomer there are many things you should take care in your script: 对于新手来说,您应该在脚本中注意许多事项:

  1. First you have to avoid the insertion of $_POST values directly into a query as it may lead to sql injection problems. 首先,您必须避免将$ _POST值直接插入查询中,因为这可能会导致sql注入问题。
  2. You should use PDO or MySQLi to perform the query for the same paranoidal reasons. 出于同样的偏执原因,您应该使用PDOMySQLi执行查询。
  3. You should be consistent. 您应该保持一致。 Why use .innerhtml when you are using JQuery? 为什么在使用JQuery时使用.innerhtml? You could use $(editableObj).html() 您可以使用$(editableObj).html()
  4. You don't show any javascript errors: Are you viewing the console? 您没有显示任何JavaScript错误:您是否正在查看控制台? Try right clicking in the body of the page and selecting "inspect element" Then click on the "console" tab of the frame that appears. 尝试右键单击页面正文,然后选择“检查元素”,然后单击出现的框架的“控制台”选项卡。
  5. Also you are not printing the "data" variable that gets returned to see if is the expected result in server. 另外,您不打印返回的“数据”变量,以查看服务器中的预期结果。 You could do it with: 您可以这样做:

    console.log(data); 的console.log(数据);

Other than that everything looks good in your script ;) 除此之外,脚本中的所有内容看起来都不错;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM