Creating a form to submit data into wordpress database

I have been trying to come up with some way to get a html form i made on a wordpress page to take the information entered and send it to custom tables in the wordpress database. I have tried calling a custom php script and writing code in the page itself and i cannot not figure out how to get it to work. I am going crazy trying to figure this out, I am about to give up. Any suggestions on where to start looking, enough i have been searching for a week, would be great.

I have tried using mysqli_connection but i keep getting issue connecting where i put (mysite.com,user,pass,db); and i get another error “cannot connect “user@anotherunknownsite.com” i have a shared ip so i wonder if it is connecting to this other site, which has NO reference in my code. I tired using local host instead of my site but nothing works.

Read More

This code tries to get some data off the db but nothing happens, same as b4 where mysite.com shows a error cant connect to user@randomsite.com and localhost doesnt work.

 <?php
    $con=mysqli_connect("mysite.com","user","pass","db");
    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM Liquor_Type");

    while($row = mysqli_fetch_array($result)) {
      echo $row['lq_name'] . " " . $row['description'];
      echo "<br>";
    }

    mysqli_close($con);
    ?>

my form

<form  action = "setLQType.php" method="post">
Add a New Liquor Type</br></br>
<p>Name: <input type="text" name="name"/></p>
<p>Description <input type="text" name="description"/></p>
----------------------------------------------
<input type="submit" name="Submit"/>
</form>
</br>

The setLQType php file, which is in the main wordpress dictionary. It will got to the file but do nothing.

<?php
$con=mysqli_connect("mysite.com","user","pass","db_name");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name = mysqli_real_escape_string($con, $_POST['name']);
$description = mysqli_real_escape_string($con, $_POST['description']);

$sql="INSERT INTO Liquor_Type(lq_name, description)
VALUES ('$name ', '$description)";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

Updated insert page… setLiquorType.php in website.com/setLiquorType.php

<?php 
$global $wpdb;
$name = $_POST['name'];
$description = $_POST['description'];
$table_name = $wpdb->prefix . "wp_Liquor_Type";
$wpdb->insert($table_name, array(
                            'lq_name' => $name,
                            'description' => $description
                            ));
?>

Related posts

Leave a Reply

2 comments

  1. Posting works like this:

    global $wpdb;
    
    $name = sanitize_text_field(  $_POST['name']);
    $lq_name = sanitize_text_field( $_POST['lq_name']);
    $description = $_POST['description'];
    $table_name = $wpdb->prefix . "Liquor_Type";
    $wpdb->insert( $table_name, array(
        'liquor_name' => $liquor_name,
        'description' => $description
    ) );
    

    Your mysql connection is available in the var $wpdb

    global $wpdb; 
    
                        $sql="SELECT * FROM Liquor_Type";
    
                        $row = $wpdb->get_results($sql);                   
                        foreach ($row as $data)
                        {
                         echo $data->lq_name. " " . $data->description;
                           echo "<br>";
                        }
    
  2. I recommend to use WP functions to do the work. You need the global object $wpdb to use them. For insert try with $wpdb->insert($table, $array).

    You can define a shortcode that you put in functions.php or make a little plugin for this. In the same function yo can paint the form and try to get the data from it (thought I prefer to get the data first and paint the form the last)

    Here you have a code that works:

    <?php
    if( $_POST['name'] != '' && is_email($_POST['email']) && wp_verify_nonce( $_POST['aspirante_nonce'], 'graba_aspirante')) {
        $table = 'Liquor_Type';
        $nombre = sanitize_text_field($_POST['nombre']);
        $correo = $_POST['correo'];
        $web = esc_url($_POST['web']);
        $created_at = date('Y-m-d H:i:s');
    
        $wpdb->insert(
            $table,
            array(
                'name' => $name,
                'email' => $email,
                'web' => $web,
                'created_at' => $created_at,
            )
        );
        echo "<p>Your data has been recorded. Thanks!<p>";   
    } else {
        echo "<p>There was an error. Try again, please!<p>";   
    }
    ob_start();
    ?>
    <form action="<?php get_the_permalink(); ?>" method="post" id="my_form">
        <?php wp_nonce_field('save_it', 'form_nonce'); ?>
        <div class="form-input">
            <label for="name">Name</label>
            <input type="text" name="name" id="name" required>
        </div>
        <div class="form-input">
            <label for='email'>Email</label>
            <input type="email" name="email" id="email" required>
        </div>
        <div class="form-input">
            <label for="web">Web</label>
            <input type="text" name="web" id="web">
        </div>
        <div class="form-input">
            <input type="submit" value="Submit">
        </div>
    </form>
    <?php
    
    return ob_get_clean();