【问题标题】:<a href=""> Action to database<a href=""> 对数据库的操作
【发布时间】:2016-02-19 01:44:43
【问题描述】:

有没有办法一键更新数据库?

echo "<a href='".$wpdb->query(" UPDATE partners SET active='no' WHERE partner_id='$active_partner->partner_id' ")."'>Disable</a>";

【问题讨论】:

  • 不是那种语法。
  • 你在这里混合你的技术。您不能使 HTML 与 PHP 交互。 PHP = 服务器端语言,HTML = 客户端标记(用于演示)
  • 你必须学习html/javascript和php之间客户端-服务器通信的基础。
  • 您可能想从一些关于 PHP 和 Wordpress 开发的介绍性教程开始。

标签: php wordpress


【解决方案1】:

您需要使用 ajax 函数对点击做出反应。

$('#your_button_id').bind('click', function () {
  function_with_ajax();
})

function function_with_ajax() {
  $.ajax({
    here you could call the update.php script and transmit dynamic data
  });
}

【讨论】:

    【解决方案2】:
    <button id="btn-save" type="button">
        save
    </button>
    <script type="text/javascript">
        jQuery(document.ready(function ($) {
    
            $("#btn-save").click(
                function () {
                    $.ajax({
                        type:"POST",
                        url:"/wp-admin/wp-ajax.php",
                        data:{
                            action:"save_data",
                            data:"data-update",
                            condition:"data-condition"
                        },
                        success:function (response) {
                            console.log(response);
    
                        },
                        error:function (error) {
                            console.log(error);
    
                        }
                    })
                }
            );
    
        }))
    
    </script>
    

    并添加到文件功能:

    add_action("wp_ajax_save_data",function ()
    {
        global  $wpdb;
        $wpdb->update("your-table",
            ["your_field" => $_POST['data']]
            ,["condition-filed"=>$_POST['condition']]);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 2018-07-25
      • 2014-01-10
      • 2011-09-16
      • 2013-05-29
      • 2011-12-06
      • 2017-12-24
      相关资源
      最近更新 更多