WordPress add to database from form

I’m trying to add content from a form to a WordPress database using $wpdb

I have used $wpdb a few time but never to update and never in conjunction with input fields and I’m having a few issues with it.

Read More

bellow is the php and the form I have written up:

<form method="post">
  <input class="totaltrsut" type="text" value="" name="totaltrsut">
  <input class="totalreviews" type="number" value="" name="totalreviews">
  <input type="hidden" name="token" value="<?php echo $newToken; ?>">
  <input class="committodb" type="submit" value="Add Stats">
</form>
<?php
 global $wpdb;
 $successa=$wpdb::update( 'dc_additional', array( 'addi_value' => $_POST['totaltrsut'] ), array( 'addi_value' => 1 ), array( '%s', '%d' ), array( '%d' ) );
 $successb=$wpdb::update( 'dc_additional', array( 'addi_value' => $_POST['totalreviews'] ), array( 'addi_value' => 2 ), array( '%s', '%d' ), array( '%d' ) );
if($successa && $successb){echo 'data has been save';}
?>

What’s supposed to happen is the form updates the additional table’s addi_value column with the corresponding information based on the add_id.

Related posts

Leave a Reply

1 comment

  1. You just need to make one change in your code.

    $wpdb is a global variable. it’s not a static class.

    WordPress $wpdb->update codex.

    ** Updated Code:**

    <form method="post">
      <input class="totaltrsut" type="text" value="" name="totaltrsut">
      <input class="totalreviews" type="number" value="" name="totalreviews">
      <input type="hidden" name="token" value="<?php echo $newToken; ?>">
      <input class="committodb" type="submit" value="Add Stats">
    </form>
    <?php
     global $wpdb;
     $successa=$wpdb->update( 'dc_additional', array( 'addi_value' => $_POST['totaltrsut'] ), array( 'addi_value' => 1 ), array( '%s', '%d' ), array( '%d' ) );
     $successb=$wpdb->update( 'dc_additional', array( 'addi_value' => $_POST['totalreviews'] ), array( 'addi_value' => 2 ), array( '%s', '%d' ), array( '%d' ) );
    if($successa && $successb){echo 'data has been save';}
    ?>