1

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 "[email protected]" 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.

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 [email protected] 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
                            ));
?>
4
  • 1
    You're going to have to give some code examples Commented Oct 17, 2014 at 19:19
  • what specifically is the error when you try to do this? could you add the stack trace? Commented Oct 17, 2014 at 19:28
  • The code you gave us only displays the contents of the Liquor_Type table. If you give us the HTML of the form you're trying to submit, as well as the php script that has the INSERT code, we can help. Commented Oct 17, 2014 at 19:34
  • here is the issue i keep getting Commented Oct 17, 2014 at 19:40

2 Answers 2

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>";
                    }
Sign up to request clarification or add additional context in comments.

6 Comments

Awesome that works thanks, but how can i take a form data and send it to the db?
will wordpress send it to a php file or do i have to have the php in the wp page?
I am still having issues, i have tired your insert method, and i have written something similar in the past, and still can't get anything to input into the db.
Time to check your error log and see what's being written there.
Ok i figured out why simple inputs were not working, i had to rename my table to wp_Liquor_Type. Now i have one more question, I cannot get my html code to send the setLiquorType.php file that i have within wp main folder. Idk if i have the code inside the wp post, how do i send the input information without doing a action="phpscript.php" in the form. U posted my input code.
|
0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.