admin管理员组

文章数量:1122832

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?

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?

Share Improve this question asked Dec 3, 2015 at 19:44 Sean McVeySean McVey 113 bronze badges 1
  • do you have debugging enabled? – Milo Commented Dec 3, 2015 at 19:59
Add a comment  | 

2 Answers 2

Reset to default 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.

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

本文标签: mysqlProcessing forms with php to wordpress database