Britchi2
Britchi2

Reputation: 37

Send form data to WordPress database table

I am trying to build a plugin that will allow the admin to add data about a particular package ( just like creating User). In the front end I will have a form that will allow the visitors to search for a particular package using an id that was assigned to that package by the admin.

I built the system using php and its working fine. Now I want to turn it into a WordPress plugin, and I am finding it hard to send the form data to wpdb table that was created when the plugin was activated (wp_demo).

This is the form:

function registration_init(){
    echo '<form action="demo.php" method="post" />';
    echo "<h1>Britchi Tracking</h1>";
    echo '<p>';
    echo 'Costumers Name (required) <br/>';
    echo '<input type="text" name="cname" placeholder="Emmanuel John"/>';
    echo '</p>';
    echo '<p>';
    echo 'Tracking number (required) <br/>';
    echo '<input type="text" name="ctracking" placeholder="EEX28PDS"/>';
    echo '</p>';
    echo '<p>';
    echo 'Email (required) <br/>';
    echo '<input type="email" name="cemail" placeholder="Email"/>';
    echo '</p>';
    echo '<p>';
    echo 'Recived Port (required) <br/>';
    echo '<input type="text" name="crport" placeholder="Brazil Port"/>';
    echo '</p>';
    echo '<p>';
    echo 'Delivered Port (required) <br/>';
    echo '<input type="text" name="cdport" placeholder="Lagos Port"/>';
    echo '</p>';
    echo '<p>';
    echo 'Current Location (required) <br/>';
    echo '<input type="text" name="clocation" placeholder="Abuja" />';
    echo '</p>';
    echo '<p>';
    echo 'Destination (required) <br/>';
    echo '<input type="text" name="cdestination" placeholder="Total filling Station, Abuja"/>';
    echo '</p>';
    echo '<p><input type="submit" name="cregistered" value="Register"></p>';
    echo '</form>';
}

This is the form processing code:

<?php

define('DB_NAME', 'wordpree2');
define('DB_USER', 'root');
define('DB_PASSWORD', 'chimezie12');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysql_error());
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
}

$db_selected = mysql_selected_db(DB_NAME, $link);

if (!$db_select) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$cname = $_POST['cname'];
$cemail = $_POST['cemail'];
$ctracking = $_POST['ctracking'];
$crport = $_POST['crport'];
$cdport = $_POST['cdport'];
$clocation = $_POST['clocation'];
$cdestination = $_POST['cdestination'];

$sql = "INSERT INTO `wp_demo` (cname, cemail, ctracking, erport, cdport, clocation, cdestination) VALUES ('$cname', '$cemail', '$ctracking', '$crport', '$cdport', '$clocation', '$cdestination')";

if (!mysql_query($sql)) {
    die('ERROR: ' . mysql_error());
}

mysql_close();
?>

I know this is not the correct way to insert data to wpdb table, but I don't know how else to do it.

Upvotes: 3

Views: 3708

Answers (1)

B_CooperA
B_CooperA

Reputation: 659

Just a short example of how to insert data in the database the Wordpress way:

<?php 
global $wpdb;
$wpdb->insert('wp_demo', [
    'cname'     => $_POST['cname'],
    'cemail'    => $_POST['cemail'],
    'ctracking' => $_POST['ctracking']
    ], ['%s','%s', '%s']
);

For full reference, see: https://codex.wordpress.org/Class_Reference/wpdb#INSERT_row

Also, don't forget to sanitize the input: https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data

Upvotes: 5

Related Questions