Iustin Beceneagă
Iustin Beceneagă

Reputation: 153

$wpdb update multiple rows in wordpress

I'm trying to update multiple rows in wordpress using $wpdb but It doesn't work! Any help would be appreciated!

$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 } ?>

Upvotes: 0

Views: 1670

Answers (2)

Hasta Dhana
Hasta Dhana

Reputation: 4719

No wonder it's get an undefined array index error since you set the dept_id input disabled. In order to preserve the dept_id values, you could setup a hidden dept_id input after the disabled input :

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

Upvotes: 1

Darsh khakhkhar
Darsh khakhkhar

Reputation: 676

You have "disabled" property in <td><input name="dept_id[]" type="text" value="<?php echo $dep['dept_id'] ?>" class="form-control" disabled></td> disabled inputs are not considered in form submit. change it to read only

Upvotes: 1

Related Questions