Abhijit Dutta Abhijit Dutta - 14 days ago 6
MySQL Question

Insert data into table after clicking submit button in wordpress

*function function_name() {

global $wpdb;
$table_name = $wpdb->prefix . 'booking';
$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE ".$table_name." (
id mediumint(11) NOT NULL AUTO_INCREMENT,
size mediumint(11) NOT NULL,
date varchar(255) DEFAULT '' NOT NULL,
time varchar(255) DEFAULT '' NOT NULL,
name varchar(255) DEFAULT '' NOT NULL,
email varchar(255) DEFAULT '' NOT NULL,
phone varchar(255) DEFAULT '' NOT NULL,
message longtext DEFAULT '' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";

require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );


}
add_action( 'after_switch_theme', 'function_name' );

function html_form_code_booking() {
echo '<form action="" method="post" class="w3-container w3-padding-48" style="background:rgba(240,240,240,0.7); padding-left:32px; padding-right:32px;">';

echo '<h1 class="w3-xxxlarge"><b>Book A Table</b>';
echo '<small style="display:block;">Lorem ipsum dolar</small></h1>';

echo '<div class="w3-row">';

echo '<div class="w3-col l4 m12 s12 w3-row">';
echo '<label class="w3-col l6 m12">Party Size</label>';
echo '<div class="w3-col l6 m12">';
echo '<input class="w3-input w3-border w3-theme" type="text" name="size" placeholder="Size" pattern="[[-+]?[0-9]*[,]?[0-9]+ ]+" value="' . ( isset( $_POST["size"] ) ? esc_attr( $_POST["size"] ) : '' ) . '" size="40" />';
echo '</div>';
echo '</div>';

echo '<div class="w3-col l4 m12 s12 w3-row w3-margin-xs" style="padding: 0 4px;">';
echo '<label class="w3-col l6 m12">Date</label>';
echo '<div class="w3-col l6 m12">';
echo '<input class="w3-input w3-border w3-theme" type="text" id="datepicker" name="date" placeholder="Date" pattern="[^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$ ]+" value="' . ( isset( $_POST["date"] ) ? esc_attr( $_POST["date"] ) : '' ) . '" size="40" />';
echo '</div>';
echo '</div>';

echo '<div class="w3-col l4 m12 s12 w3-row">';
echo '<label class="w3-col l6 m12">Time</label>';
echo '<div class="w3-col l6 m12">';
echo '<select class="w3-input w3-border w3-theme" name="time" value="' . ( isset( $_POST["time"] ) ? esc_attr( $_POST["time"] ) : '' ) . '">
<option value="12:00 AM">12:00 AM</option>
<option value="12:30 AM">12:30 AM</option>
<option value="01:00 AM">01:00 AM</option>
<option value="01:30 AM">01:30 AM</option>
<option value="02:00 AM">02:00 AM</option>
<option value="02:30 AM">02:30 AM</option>
<option value="03:00 AM">03:00 AM</option>
<option value="03:30 AM">03:30 AM</option>
<option value="04:00 AM">04:00 AM</option>
<option value="04:30 AM">04:30 AM</option>
<option value="05:00 AM">05:00 AM</option>
<option value="05:30 AM">05:30 AM</option>
<option value="06:00 AM">06:00 AM</option>
<option value="06:30 AM">06:30 AM</option>
<option value="07:00 AM">07:00 AM</option>
<option value="07:30 AM">07:30 AM</option>
<option value="08:00 AM">08:00 AM</option>
<option value="08:30 AM">08:30 AM</option>
<option value="09:00 AM">09:00 AM</option>
<option value="09:30 AM">09:30 AM</option>
<option value="10:00 AM">10:00 AM</option>
<option value="10:30 AM">10:30 AM</option>
<option value="11:00 AM">11:00 AM</option>
<option value="11:30 AM">11:30 AM</option>
<option value="12:00 PM">12:00 PM</option>
<option value="12:30 PM">12:30 PM</option>
<option value="01:00 PM">01:00 PM</option>
<option value="01:30 PM">01:30 PM</option>
<option value="02:30 PM">02:30 PM</option>
<option value="03:00 PM">03:00 PM</option>
<option value="03:30 PM">03:30 PM</option>
<option value="04:00 PM">04:00 PM</option>
<option value="04:30 PM">04:30 PM</option>
<option value="05:00 PM">05:00 PM</option>
<option value="05:30 PM">05:30 PM</option>
<option value="06:00 PM">06:00 PM</option>
<option value="06:30 PM">06:30 PM</option>
<option value="07:00 PM">07:00 PM</option>
<option value="07:30 PM">07:30 PM</option>
<option value="08:00 PM">08:00 PM</option>
<option value="08:30 PM">08:30 PM</option>
<option value="09:00 PM">09:00 PM</option>
<option value="09:30 PM">09:30 PM</option>
<option value="10:00 PM">10:00 PM</option>
<option value="10:30 PM">10:30 PM</option>
<option value="11:00 PM">11:00 PM</option>
<option value="11:30 PM">11:30 PM</option>
</select>';
echo '</div>';
echo '</div>';

echo '</div>';

echo '<div class="w3-row w3-margin-top w3-margin-bottom">';
echo '<label class="w3-col l2 s12">Name</label>';
echo '<div class="w3-col l10 s12">';
echo '<input class="w3-input w3-border w3-theme" type="text" name="name" placeholder="Your Name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["name"] ) ? esc_attr( $_POST["name"] ) : '' ) . '" size="40" />';
echo '</div>';
echo '</div>';

echo '<div class="w3-row w3-margin-top w3-margin-bottom">';
echo '<label class="w3-col l2 s12">Email</label>';
echo '<div class="w3-col l10 s12">';
echo '<input class="w3-input w3-border w3-theme" type="email" name="email" placeholder="Your Email" value="' . ( isset( $_POST["email"] ) ? esc_attr( $_POST["email"] ) : '' ) . '" size="40" />';
echo '</div>';
echo '</div>';

echo '<div class="w3-row w3-margin-top w3-margin-bottom">';
echo '<label class="w3-col l2 s12">Phone</label>';
echo '<div class="w3-col l10 s12">';
echo '<input class="w3-input w3-border w3-theme" type="text" name="phone" placeholder="Your Phone" pattern="[^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$ ]+" value="' . ( isset( $_POST["phone"] ) ? esc_attr( $_POST["phone"] ) : '' ) . '" size="40" />';
echo '</div>';
echo '</div>';

echo '<div class="w3-row w3-margin-top w3-margin-bottom">';
echo '<label class="w3-col l2 s12">Message</label>';
echo '<div class="w3-col l10 s12">';
echo '<textarea class="w3-input w3-border w3-theme" placeholder="Your Message" style="resize:none" rows="4" name="message">' . ( isset( $_POST["message"] ) ? esc_attr( $_POST["message"] ) : '' ) . '</textarea>';
echo '</div>';
echo '</div>';

echo '<div class="w3-right-align"><input class="w3-btn w3-white w3-border w3-border-cs w3-round" type="submit" name="submitted" value="Submit"></div>';
echo '</form>';

}

if(isset($_POST['submitted'])) {

$size = $_POST['size'];
$date = $_POST['date'];
$time = $_POST['time'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

$wpdb->insert(
$table_name,
array(
'size' => $size,
'date' => $date,
'time' => $time,
'name' => $name,
'email' => $email,
'phone' => $phone,
'message' => $message,
)
);
}

function deliver_mails() {

// if the submit button is clicked, send the email


// sanitize form values
//$size = sanitize_text_field( $_POST["size"] );
//$date = sanitize_text_field( $_POST["date"] );
//$time = sanitize_text_field( $_POST["time"] );
//$name = sanitize_text_field( $_POST["name"] );
//$email = sanitize_email( $_POST["email"] );
//$phone = sanitize_text_field( $_POST["phone"] );
//$subject = "Table Booking";
//$message = esc_textarea( $_POST["message"] );



// get the blog administrator's email address
$to = get_option( 'admin_email' );

$headers = "From: $name <$email>" . "\r\n";

// If email has been process for sending, display a success message
if ( wp_mail( $to, $subject, $message, $headers ) ) { ?>
<div class="w3-panel w3-green">
<span onclick="this.parentElement.style.display='none'" class="w3-closebtn">×</span>
<p>Mail send successfully.</p>
</div>
<?php
} else { ?>
<div class="w3-panel w3-red">
<span onclick="this.parentElement.style.display='none'" class="w3-closebtn">×</span>
<p>Please fill in all the required fields.</p>
</div>
<?php
}
}


function booking_shortcode() {
ob_start();
deliver_mails();
html_form_code_booking();

return ob_get_clean();
}

add_shortcode( 'booking_shortcode', 'booking_shortcode' );*


Here is my full code. I am not able to insert data into table in wordpress. I need help for that. I'm new in wordpress theme development. Data not inserted into my table.Check out carefully and please modify my code. Please help me out.

Answer

What means 'Sorting with pagination'?

When you get data from your database, let's say, using SQL, you may get already sorted data.

Something like SELECT * FROMTABLEORDER BYSOMETHINGASC