douzong5473 2017-07-12 03:50
浏览 49
已采纳

更新wordpress自定义表

I am building a dashboard with custom User Data. Each element on the dashboard goes into the loop and shows the related information regarding that user.I am able to show the the custom field using session and loops but i need to update the information as well, somehow it is not updating. Please help with this:

<?php 
global $wpdb;
$table_name = $wpdb->prefix . 'contract';
$wsid = $_SESSION['wedding-values'];
$results = $wpdb->get_results( "SELECT * FROM $table_name WHERE id=$wsid " );
if ( $results ) {
    foreach ( $results as $result ){
        $wcname=$result->cname;
         $wctname=$result->ctname;
          $waddress=$result->address;
           $wmobile=$result->mobile;
            $wemail=$result->email;
             $wdate=$result->Date;
             $id=$result->id;

    }

}
if ( isset( $_POST['submit'] ) ){

 $wpdb->update($table_name,array(
    'cname'=>$_POST['cname'],array('id'=>$wsid))

       );

}

 ?>

Html:

<div id="col-md-9">
    <form action="" method="post">
    <table>    
    <tr>
        <td><p style="color: red"> Couple Name </p></td>
        <td><input type=text name=cname placeholder=<?php echo $wcname;?>></td>

    </tr>
    <tr>
        <td><p style="color: red"> Contact Name </p></td>
        <td><?php echo $wctname;?></td>

    </tr>
    <tr>
        <td><p style="color: red"> Address </p></td>
        <td><?php echo $waddress;?></td>

    </tr>
    <tr>
        <td><p style="color: red"> Mobile </p></td>
        <td><?php echo $wmobile;?></td>

    </tr>
    <tr>
        <td><p style="color: red"> Email</p></td>
        <td><?php echo $wemail;?></td>

    </tr>
    <tr>
        <td><p style="color: red"> Date Submitted</p></td>
        <td><?php echo $wdate;?></td>

    </tr>
    <tr>

        <td><input type="submit" name="editsave"  value="Edit and Save" ></td>

    </tr>

  </table>
  </form>
    </div>  
  • 写回答

2条回答 默认 最新

  • duankuiyu4618 2017-07-12 05:09
    关注

    You were forget to add this 2 things in query,

    $table_name,array('cname'=>$_POST['cname']),array('id'=>$wsid),array('%s'),array('%d'))

    array('%s'),array('%d')

    Check this example

    $result = $wpdb->update($table_name,array(
        'cname'=>$_POST['cname']),array('id'=>$wsid),array('%s'),array('%d'))
    );
    
    if($result > 0){
       echo "Successfully Updated";
    }
    else{
      exit( var_dump( $wpdb->last_query ) );
    }
    $wpdb->flush();
    

    EDIT

    Also change this

    if (isset($_POST['submit']))
    

    To

    if (isset($_POST['editsave']))
    

    You were using wrong submit button name

    Try with this code. Hope this will helps you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?