0

I am trying to insert some data in database which will be received from a form but the array of $wpdb is blank. There is no error reported by WordPress still the records are not been inserted. This page is not in admin panel. I am adding this code in a page called holiday-list through creating a short code while the plugin is been activated.

Below is the code.

<?php 
function form_creation(){
global $table_prefix, $wpdb;
$table_name = $table_prefix . "holiday";
$state_table= $table_prefix . "state";
?>
  <div>

  <h2>Holiday List</h2>
  <?php
 if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit"])){
  ?>
  <h2>Add Holiday</h2>
  <form name="add-form" action="<?php echo get_home_url(); ?>/holiday-list" method="POST">
      <input type="text" name="holiday_name" placeholder="Name of the Holiday" required>
      <input type="date" name="date" placeholder="select date" required>
      <select name="state_form_select" required>
          <option value="none">None</option>
          <?php
          $results = $wpdb->get_results( "SELECT id,state_name FROM $state_table ORDER BY state_name ASC"); // Query to fetch data from database table and storing in $results


    foreach($results as $row){   
        ?>
          <option value="<?php echo $row->id; ?>"><?php echo $row->state_name; ?></option>;
        <?php
    }

          ?>

      </select>
      <input type="submit" name="state_submit">
  </form>

  <?php
  }
   else if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["state_submit"])){

      $holiday_name = $_POST['holiday_name'];
      $date = $_POST['date'];
      $state_form_select = $_POST['state_form_select'];

      $wpdb->insert( 
    $table_name, 
    array( 
        'holiday_name' => $holiday_name, 
        'date' => $date, 
        'state' => $state_form_select, 
    ) 
);
      $success = $wpdb->insert($table_name, array(
   "holiday_name" => $holiday_name,
   "date" => $date,
   "state" => $state_form_select,
));

 if($success) {
     echo $success;
 echo ' Inserted successfully';
      } else {

   echo 'not';
   }
}



  else{
    echo $table_name;
  ?>
  <form name="add-button" action="http://connect.narayanbhargavagroup.loc/holiday-list/" method="POST">
      <input type="submit" name="submit" value="Add Holiday">
      </form>
  <table width="100%" border="1" cellpadding="5" cellspacing="0">
      <thead>
          <tr>
      <td>Sr.No</td>
      <td>Holiday</td>
      <td>Date</td>
      <td>Day</td>
      <td>State</td>
          </tr>
      </thead>
      <tbody>
          <tr>
          <?php
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results


    foreach($results as $row){   
        ?>
        <td><?php echo $row->id; ?></td>
        <td><?php echo $row->holiday_name;?> </td>
        <td><?php echo $row->date; ?></td>
        <td><?php echo $row->day; ?></td>
        <td><?php echo $row->states; ?></td>
        <?php
    }
?>
          </tr>
      </tbody>

      </thead>
  </table>
  </div>

<?php
  }
}
add_shortcode('test', 'form_creation');

Below is the create table syntax i am using code so for table structure understanding.

CREATE TABLE `connect_holiday` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `holiday_name` varchar(250) NOT NULL,
  `date` varchar(128) NOT NULL,
  `day` varchar(50) NOT NULL,
  `states` varchar(250) NOT NULL,
  `addedby` varchar(250) NOT NULL,
  `added_on` varchar(250) NOT NULL,
  `last_edited_by` varchar(250) NOT NULL,
  `timestamp` varchar(250) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  • In above code seems $table_name is not specified. – 1naveengiri Aug 16 '18 at 13:58
  • I am sorry @1naveengiri. It was specified but i did not show the hole code in the page. I have edited the post now. Thank you – Pratik Aug 16 '18 at 14:03
  • can you also share table structure you used? – 1naveengiri Aug 16 '18 at 14:06
  • @1naveengiri have updated the code used to create the table. – Pratik Aug 16 '18 at 14:13
0

In the table, all columns are set with NOT NULL consent. that means they can not be empty at all and considered as required fields.

But in the insert query, You are saving only 3 records.

$wpdb->insert( 
    $table_name, 
    array( 
        'holiday_name' => $holiday_name, 
        'date' => $date, 
        'state' => $state_form_select, 
    ) 
);

That's the issue.

Either insert all required values in the table or add proper consent.

  • No bro, i tried adding all the field with some value but it is still shoing the same issue. – Pratik Aug 16 '18 at 15:04
  • $wpdb->insert( $table_name, array( 'id' => ' ', 'holiday_name' => $holiday_name, 'date' => $date, 'day' => 'testvalue', 'state' => $state_form_select, 'addedby' => $user_id_table, 'added_on' => 'now', 'last_edited_by' => 'testvalue', 'timestamp' => 'now', ) ); – Pratik Aug 16 '18 at 15:05
  • are you sure the values within column data limit. – 1naveengiri Aug 16 '18 at 15:22
  • Yes @1naveengiri the value is within column data limit – Pratik Aug 17 '18 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.