0

I'm simply trying to process data from an html form into my wordpress database through php. I've tried multiple times to get the data inserted but have had no luck.

The setup is as follows: Database name: seanmcve_AssociationNation Table: association The row I want data entered in: association_first_name

I've setup a page template called form_processing_template.php.

<?php
/*
Template Name: form processing template
*/
?>

<form action="" method="post">

<h4><strong>What is the legal name of your association?</strong></h4>
<input name="legalName" type="text" placeholder="Association Name" />
<input type="submit" name="submit" value="Submit" />
</form>

<?php
If($_POST['submit']) {
// run validation if you're not doing it in js

$legalname=$_POST['legalName'];
echo $legalname;
$wpdb->insert('association',
        array(
                'association_first_name' => '$legalname'

            ),
            array(
             '$s',
             )
);

}
?>

<?php x_get_view( x_get_stack(), 'wp', 'page' ); ?>

I'm echoing the variable just to make sure it's correct and working (which is working) I've been referencing the wpdb class reference page and still have had no luck. Can anyone point me in the right direction?

1

2 Answers 2

0

I'm pretty sure you don't quote the $variable at all when doing a query with wpdb.

So 'association_first_name' => '$legalname' would be 'association_first_name' => $legalname

But that might not solve all your problems.

0
global $wpdb;
$table_name = $wpdb->prefix . 'association';
$wpdb->insert($table_name,
        array(
                'association_first_name' => $legalname
        ),
        array(
                '%s',
        )
);

You have multiple issues:

  1. Need to set table prefix
  2. You need to init $wpdb (global $wpdb)
  3. its %s not $s
  4. $legalname without ''

Use the Snippet above to handle the insert part

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.