I am using a form to add data in table using plugin in wordpress but when i enters submitt button it shows nothing and no data add in table where i doing mistake?? plz anybody help me. My code for insert data is here...
<form id="form1" name="form1" method="post" action="">
Name
<input type="text" name="name" /> </br>
Website
<input type="text" name="website" /> </br>
Description
<input type="text" name="description" /> </br>
<input type="submit" value="Submit Form" />
</form>
<?php
global $wpdb;
$wpdb->query($structure);
$wpdb->query="insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')";
if (is_object($wpdb) && is_a($wpdb, 'wpdb')) {
if (!$wpdb->insert('.PRO_TABLE_PREFIX.tutorial',
array(
'name'=>$_POST[name]
,'website'=>$_POST[website]
,'description'=>$_POST[description]
))) exit;
}
else {echo 'Form Submitted';}
?>
A blank page usually means an internal 50x error, normally caused by PHP or your web hosting software (which is likely Apache).
$wpdb->query="insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')";
$wpdb->query=
is not valid. The code should read :
$wpdb->query("insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')");
because $wpdb->query is a function, not a variable.
Update: (More in-depth)
First, let me start by linking to the wpdb documentation. For your purpose, you'll want to do this:
$table = $wpdb->prefix."my_table";
Note: When typing in the table name, don't include the "wp_" prefix. The "wp_" prefix can be changed by a number of things, but it will always be stored in $wpdb->prefix
, so always use this instead of typing the default prefix.
global $wpdb;
$wpdb->insert($table,array(
"name" => mysql_real_escape_string($_POST['name']),
"website" => mysql_real_escape_string($_POST['website']),
"description" => mysql_real_escape_string($_POST['description'])
));
That will enter the record into your database. mysql_real_escape_string is important to protect yourself against MYSQL Injection. That's about all there is to it.
Update 2: (response to next comment)
Then you need to have PHP check to see if the form is submitted. You could simply add if(isset($_POST)){}
, but I personally don't like doing that because if another form submitted to this page via post, the database would still update.
<?php if(!isset($_POST[PLUGIN_PREFIX.'submit'])){
global $wpdb;
$table = $wpdb->prefix.PLUGIN_PREFIX."my_table";
// $wpdb->insert will return true or false based on if the query was successful.
$success = $wpdb->insert($table,array(
"name" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'name']),
"website" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'website']),
"description" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'description'])
));
if($success){
$display = '<div class="'.PLUGIN_PREFIX.'submit_success">
Your data has been saved.
</div>';
}else{
$display = '<div class="'.PLUGIN_PREFIX;.'submit_fail">
Your data failed to save. Please try again.
</div>';
}
}
$display .= '<form id="form1" name="form1" method="post" action="">
<label for="name">Name</label>
<input id="name" type="text" name="'.PLUGIN_PREFIX.'name" /> </br>
<label for="website">Website</label>
<input id="website" type="text" name="'.PLUGIN_PREFIX.'website" /> </br>
<label for="description">Description</label>
<input id="description" type="text" name="'.PLUGIN_PREFIX.'description" /> </br>
<input type="hidden" name="'.PLUGIN_PREFIX.'submit" />
<input type="submit" value="Submit Form" />
</form>';
return $display;
A few things I added that seem to be part of good plugin development:
define('PLUGIN_PREFIX', 'plugin_name_');
. Use the prefix before anything that may conflict with other plugins.<label>
tags are very important. if the label's "for" matches an input's "id", clicking the label will select that input.