0

i created a dbdelta table but i cant seem to insert any data here is my code please help

function lapizzeria_save_reservation() {
  global $wpdb;
  if(isset($_POST['reservation']) && $_POST['hidden'] =="1")  {
    $name =$_POST['name'];
    $date =$_POST['date'];
    $email =$_POST['email'];
    $phone =$_POST['phone'];
    $message =$_POST['message'];

    $table = $wpdb->prefix . 'reservations';

    $data = array(
      'name' => $name,
      'date' => $date,
      'email' => $email,
      'phone' => $phone,
      'message' => $message
    );

    $format = array(
      '%s',
      '%s',
      '%s',
      '%s',
      '%s'
    );
    $wpdb->insert($table, $data, $format);

  }
}

add_action('init', 'lapizzeria_save_reservation');

here is the html

<form class="reservation-form" method="post">
                <h2>Make a Reservation</h2>
                <div class="field">
                  <input type="text" name="name" placeholder="Name" required>
                </div>
                <div class="field">
                  <input type="datetime-local" name="date" placeholder="Date" required>
                </div>
                <div class="field">
                  <input type="text" name="email" placeholder="E-mail">
                </div>
                <div class="field">
                  <input type="tel" name="phone" placeholder="Phone Number" required>
                </div>
                <div class="field">
                  <textarea name="message" placeholder="Message" requires></textarea>
                </div>
                <input type="submit" name="reservation" class="button" value="Send">
                <input type="hidden" name="hidden" value="1">
            </form>
  • 1
    I'm not sure init is the best place to process posts - maybe admin_post? Or a form plugin might be a better starting point and hook form submission there. What's going wrong: is this code never executing, or are you getting SQL errors, or something else? – Rup Dec 29 '19 at 12:43
  • the code is not executing, i think init is not working, can you help me please, thank you – Arvin Bahilango Jan 6 at 8:14
0

I would strongly suggest using AJAX for this purpose.

Using "init" for inserting some data to your database is not a good solution.

Here's a good guide on how to do it with AJAX: https://webprogramo.com/how-to-create-an-ajax-form-in-wordpress/1156/

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.