最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wpdb - How to update a row in a table in Wordpress

programmeradmin1浏览0评论

I have a table of data rows. Now I want to click on the Update button then that row of data will be Updated based on the ID

Here is my demo:

These are my commands:

<?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 );
                }

            ?>

I'm stuck here for hours, please help me! Thanks!

I have a table of data rows. Now I want to click on the Update button then that row of data will be Updated based on the ID

Here is my demo:

These are my commands:

<?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 );
                }

            ?>

I'm stuck here for hours, please help me! Thanks!

Share Improve this question edited Jan 28, 2020 at 15:46 Tung Nguyen asked Jan 28, 2020 at 15:38 Tung NguyenTung Nguyen 133 bronze badges 2
  • Move echo '</form>'; inside the foreach, so that each row is its own form. At the moment all the fields are going into one form, which is preventing your code from working because there will be multiple IDs. As long as you've got the table name/structure correct, I don't see any other issue. – Jacob Peattie Commented Jan 28, 2020 at 15:44
  • Wow, It worked. Thank you so much, I've been sitting with that problem for hours without finding a fault, you helped me fix it quickly. – Tung Nguyen Commented Jan 28, 2020 at 16:01
Add a comment  | 

1 Answer 1

Reset to default 0

pull form closing tag inside foreach loop

<?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 );
}
?>
发布评论

评论列表(0)

  1. 暂无评论