如何在WordPress中更新表中的行

时间:2020-01-28 作者:Tung Nguyen

我有一个数据行表。现在我想单击更新按钮,然后该行数据将根据ID进行更新

这是我的演示:

enter image description here

以下是我的命令:

<?php
              foreach ($get_data as $infocontact) {
                    echo \'<form class="form-info-wmtp" action="" method="post" enctype="multipart/form-data">\';
                    echo \'<input type="hidden" name="id_image" id="id_image" value="\'.$infocontact->id.\'" />\';
                    echo \'<div class="txt-info-wmtp">\'.$infocontact->button_name.\'<p><input type="text" value="\'.$infocontact->link_button.\'" name="link_button" placeholder="Enter the link button">\';
                    echo \'<input type="submit" id="btnSubmitSocial" name="btnSubmitSocial" value="Update">\';
                    echo \'<input type="submit" id="delete-btn" name="btnDelete" value="Delete"></p></div>\';
                }
                echo \'</form>\';

                if(isset($_POST[\'btnSubmitSocial\'])){
                    $link = $_POST[\'link_button\'];
                    $id_img = $_POST[\'id_image\'];
                    var_dump($id_img);

                    $table = $wpdb->prefix . \'call_button\';
                    $post_data=array(
                        \'link_button\' => $link,
                        \'id\' => $id_img
                    );

                    $wpdb->update( $table, $post_data, array( \'id\' => $id_img ), $format = null, $where_format = null );
                }

            ?>
我被困在这里好几个小时了,请帮帮我!谢谢

1 个回复
最合适的回答,由SO网友:Sanaullah Ahmad 整理而成

在foreach循环内拉动表单结束标记

<?php
foreach ($get_data as $infocontact) {
        echo \'<form class="form-info-wmtp" action="" method="post" enctype="multipart/form-data">\';
        echo \'<input type="hidden" name="id_image" id="id_image" value="\'.$infocontact->id.\'" />\';
        echo \'<div class="txt-info-wmtp">\'.$infocontact->button_name.\'<p><input type="text" value="\'.$infocontact->link_button.\'" name="link_button" placeholder="Enter the link button">\';
        echo \'<input type="submit" id="btnSubmitSocial" name="btnSubmitSocial" value="Update">\';
        echo \'<input type="submit" id="delete-btn" name="btnDelete" value="Delete"></p></div>\';
        //i pull /form inside loop here
        echo \'</form>\';
}

if(isset($_POST[\'btnSubmitSocial\'])){
   $link = $_POST[\'link_button\'];
   $id_img = $_POST[\'id_image\'];
   var_dump($id_img);

   $table = $wpdb->prefix . \'call_button\';
   $post_data=array(
   \'link_button\' => $link,
   \'id\' => $id_img
   );

   $wpdb->update( $table, $post_data, array( \'id\' => $id_img ), $format = null, $where_format = null );
}
?>