为什么更新和删除查询在定制表中不起作用?

时间:2020-12-18 作者:vishal

这是自定义模板。我编写crud时使用wordpress查询发送数据库自定义表中的数据并从数据库中获取数据,插入查询工作,但更新和删除查询不工作。下面我给出更新和删除查询,请建议是否有人知道?

数据已插入,但刷新时,数据会自行删除,而不会更新或删除这是插入查询

<?php
/*
 * Template Name:Form Template page
 */
get_header(); ?>
  <?php
        global $wpdb;
        $table= \'wp_contact_form\';
         if (isset($_POST[\'submit\'])) {
        $data = array(
            \'names\' => $_POST[\'yourname\'],
            \'emails\'    => $_POST[\'customer_email\'],
            \'gender\' => $_POST[\'customer_gender\'],
            \'age\' => $_POST[\'customer_age\']
        );

        $success=$wpdb->insert( $table, $data );
        if($success){
            echo \'data has been save\' ; 
        }
    else {
            echo "data not save";
        }
        } 
        ?>
        <div class="container">
            <div class="row ">
                <div class="col-md-12 pb-5">
                    <form method="post">
                        Your name:<input type="text" name="yourname" id="yourname">
                        Your Email:<input type="text" id="customer_email" name="customer_email"/><br>
                            Gender:<input type="radio" name="customer_gender" value="male">Male
                                   <input type="radio" name="customer_gender" value="female">Female<br>
                            Your Age:<input type="text" name="customer_age" id="customer_age"><br>
                            <input type="submit" name="submit" value="Submit">
                           <input type="submit" name="show" value="Show"> <a href="http://localhost/vishal_nov/edit-form/">show</a>
                    </form>
                </div>
            </div>
        </div>
        
<?php
    get_footer();   
?>


这是更新和删除查询

<?php
 /* 
  * Template Name: Show Form Template Page
  */
?>
 <div class="container">
    <div class="row">
        <div class="col-md-12">
            <form method="POST">
            <table border="1">
                <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Email</th>
                <th>Gender</th>
                <th>Age</th>
                <th>Edit</th>
                <th>Delete</th>
                </tr> 
                <?php
                global $wpdb;
                $table= \'wp_contact_form\';
                $result = $wpdb->get_results ( "SELECT * FROM $table" );

                if(!empty($result)){
                foreach ( $result as $print ) {                                         
        ?>
                <tr>
                <td><?php echo $print->id;?></td>
                <td><?php echo $print->names;?></td>
                <td><?php echo $print->emails;?></td>
                <td><?php echo $print->gender;?></td>
                <td><?php echo $print->age;?></td>
                <td><button class="" name="update">Edit</button></td>
                <td><a href="#" id="\'.$print->id.\'" class="del_btn">Delete</a></td>
                </tr>
                <?php
                }
                } 
                ?>
        </table>
        </form>
                <?php
                global $wpdb;
                $id= $print->id;
                $table= \'wp_contact_form\';
                if (isset($_POST[\'update\'])) {
                    $data =array(
                            \'names\' => $_POST[\'yourname\'],
                            \'emails\'    => $_POST[\'customer_email\'],
                            \'gender\' => $_POST[\'customer_gender\'],
                            \'age\' => $_POST[\'customer_age\']
                                );
                    $wherecondition=array(
                                    \'id\'=>$id
                                );
                $updated=$wpdb->update($table, $data, $wherecondition);

            }   
                
                
                    ?>
        </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>

$(document).ready(function(){
                 
                 $(\'.del_btn\').click(function(){
                   var el = this;
                               
                   var deleteid = $(this).attr(\'id\');
                 
                   var confirmalert = confirm("Are you sure?");
                   if (confirmalert == true) {
                      // AJAX Request
                      $.ajax({
                        url: \'delete1.php\',
                        type: \'POST\',
                        data: { dell_id:deleteid },
                        success: function(response){

                          if(response == 1){
                        
                        $(el).closest(\'tr\').css(\'background\',\'tomato\');
                        $(el).closest(\'tr\').fadeOut(800,function(){
                           $(this).remove();
                        });
                          }
                          else{
                        alert(\'Invalid ID.\');
                          }

                        }
                      });
                  }
                   });

                 });
</script>
    </div>
</div>
在delete中定义delete查询。php文件,但显示未找到的页面

    <?php
global $wpdb;
$table= \'wp_contact_form\';
$id=$_POST[\'dell_id\'];
$result = $wpdb->delete($table, array(\'id\' => $id));

if(!empty($result))
 {
     if($result){
    echo "success";
     }
   
     
}

?>
这是屏幕截图https://prnt.sc/wbgzgt以下是控制台中未找到ajax jquery错误页面的屏幕截图https://prnt.sc/wc1l4s

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

在您定义的线路上$id:

<?php
$id= $result->id;
global $wpdb;
你得到了$id 字段输入$result. $result 是从数据库返回的对象数组get_results 呼叫看起来您想使用$print 循环中当前行的变量,因此$id = $print->id.

相关推荐

在网页中通过快捷码获取MySQL数据并显示结果的问题

我正在学习开发一个自定义插件,目前它的目的是从wp数据库读取数据并显示数据。我相信我最初的SQL语句是正确的,因为它获取了我想要显示的数据。然而,每当执行该函数时(从页面加载时的短代码),我都会得到错误,而不是显示提取的数据Trying to get property \'num_rows\' of non-object in C:\\wamp64\\www\\testsite1\\wp-content\\plugins\\WickCustomLD\\WickCustomLD.php on line 84