$ wpdb在wordpress中更新多行

时间:2019-03-05 04:16:00

标签: wordpress

我正在尝试使用$ wpdb在wordpress中更新多行,但这行不通!任何帮助将不胜感激!

$deps = $wpdb->get_results( "SELECT * FROM  $departments", ARRAY_A);
$deps_var = $wpdb->get_var( "SELECT count(*) FROM  $departments");
...
for($i=0;$i<$deps_var;$i++)
    {
    $updatedeps=array(
        'dept_name' => $_POST['dept_name'][$i], 
        'dept_contact_name' => $_POST['dept_contact_name'][$i],
        'dept_contact_email' => $_POST['dept_contact_email'][$i],
        'dept_contact_tel' => $_POST['dept_contact_tel'][$i],
        'dept_contact_fax' => $_POST['dept_contact_fax'][$i], 
        'dept_status' => $_POST['dept_status'][$i],
    ) ;
    $where=array('dept_id' => $_POST['dept_id'][$i] );
    $wpdb->update( $departments, $updatedeps, $where);
    }

<?php foreach ( $deps as $dep ) { ?>
       <tr>
        <td><input name="dept_id[]" type="text" value="<?php echo $dep['dept_id'] ?>" class="form-control" disabled></td>
        <td><input name="dept_name[]" type="text" value="<?php echo $dep['dept_name'] ?>" class="form-control"></td>
        <td><input name="dept_contact_name[]" type="text" value="<?php echo $dep['dept_contact_name'] ?>" class="form-control"></td>
        <td><input name="dept_contact_email[]" type="text" value="<?php echo $dep['dept_contact_email'] ?>" class="form-control"></td>
        <td><input name="dept_contact_tel[]" type="text" value="<?php echo $dep['dept_contact_tel'] ?>" class="form-control"></td>
        <td><input name="dept_contact_fax[]" type="text" value="<?php echo $dep['dept_contact_fax'] ?>" class="form-control"></td>
        <td><input name="dept_status[]" type="text" value="<?php echo $dep['dept_status'] ?>" class="small-text"></td>
      </tr> <?php } ?>

2 个答案:

答案 0 :(得分:1)

难怪由于您将dept_id输入设置为禁用,因此会出现未定义的数组索引错误。为了保留dept_id的值,您可以在禁用的输入之后设置一个隐藏的dept_id输入:

<?php foreach ( $deps as $dep ) { ?>
   <tr>
    <td><input type="text" value="<?php echo $dep['dept_id'] ?>" class="form-control" disabled></td>
    <td><input name="dept_id[]" type="hidden" value="<?php echo $dep['dept_id'] ?>" ></td>
    <td><input name="dept_name[]" type="text" value="<?php echo $dep['dept_name'] ?>" class="form-control"></td>
    <td><input name="dept_contact_name[]" type="text" value="<?php echo $dep['dept_contact_name'] ?>" class="form-control"></td>
    <td><input name="dept_contact_email[]" type="text" value="<?php echo $dep['dept_contact_email'] ?>" class="form-control"></td>
    <td><input name="dept_contact_tel[]" type="text" value="<?php echo $dep['dept_contact_tel'] ?>" class="form-control"></td>
    <td><input name="dept_contact_fax[]" type="text" value="<?php echo $dep['dept_contact_fax'] ?>" class="form-control"></td>
    <td><input name="dept_status[]" type="text" value="<?php echo $dep['dept_status'] ?>" class="small-text"></td>
  </tr> <?php } ?>

答案 1 :(得分:1)

您在“已禁用” 属性中 <td><input name="dept_id[]" type="text" value="<?php echo $dep['dept_id'] ?>" class="form-control" disabled></td> 禁用的输入不在表单提交中考虑。将其更改为只读