Shreyas Achar
Shreyas Achar

Reputation: 1435

Insert data from wordpress to mysql

I am a newbie to wordpress. I have a form in one of the pages like this:

 <form action="" method="post">
 Firstname: <input type="text" name="fname" /><br><br>
 Lastname: <input type="text" name="lname" /><br><br> 
  <input type="submit" />
  </form>

Now I need to store the values of textboxes to the mysql database.

Do I need to create a new php file? If so then where do I place it? What will be my php code for insertion?

Upvotes: 2

Views: 11030

Answers (5)

Shankar Thiyagaraajan
Shankar Thiyagaraajan

Reputation: 1815

In Wordpress, you need to use "Action" to submit your data to backend.

For Direct Form Submission:

  • Hooks Used

    // 'action_name' is your form_name or any, but specify uniquely.
    
    // For Authorized User.
    add_action( 'admin_post_nopriv_action_name', 'call_back_function` );
    
    // For Guest User.
    add_action( 'admin_post_action_name', 'call_back_function` );
    
  • In Form

    You must specify this inside of your Form.

    <form> ......
    <input type="hidden" name="action" value="action_name">
    ...... </form>
    

    This tells wordpress to connect your form with corresponding CallBack Function

For Ajax Form Submission:

  • Hooks Used

    add_action('wp_ajax_contact_form', 'action_name');
    add_action('wp_ajax_nopriv_contact_form', 'action_name');
    
  • List item

    You have to add another item, named as "action"

    ex.

     $.ajax({ 
     data: {action: 'action_name'},
     type: 'post',
     url: your_url,
     success: function(data) {
        // On Success
    }
    

    });

These all are the way to submitting form and manage data in wordpress.

In your callback function, you can receive data in $_GET, $_POST or $_REQUEST to receive data.

After, inset into database have multiple options,

  • You can use Post to Customize and follow the native of wordpress. Study, how to create post.
  • If you manage less data, you can choose add option

  • If you need except this, you need create new table and follow core database management with wordpress instances. Then study these WPDB and Table Creation.

    Have a Joy With Wordpress... Thank You !

Upvotes: 0

Sona
Sona

Reputation: 103

// in your template file add this code
<? php
if (isset($_REQUEST['register'])) {
    do_action('form_data');
} ?>
< form method = "post" >
    Firstname: < input type = "text"
name = "fname" / > < br > < br >
    Lastname: < input type = "text"
name = "lname" / > < br > < br >
    < input type = "submit"
class = "button"
name = "register"
value = "Submit" >
    < /form>

// and add this in function.php
<? php

function form_data_func() {
    $fname = $_REQUEST['fname'];
    $lname = $_REQUEST['lname'];
    $userdata = array(
        'first_name' => $fname,
        'last_name' => $lname,
    );
    $user_id = wp_insert_user($userdata);
}
add_action('form_data', 'form_data_func'); ?>

//developer.wordpress.org/reference/functions/wp_create_user refer this link for parameters i had shown you how to perform insertion function

Upvotes: 1

user6533277
user6533277

Reputation:

HTML

<form action="" method="post">
   Firstname: <input type="text" name="fname" /><br><br>
   Lastname: <input type="text" name="lname" /><br><br> 
   <input type="submit" name="submit" />
</form>

PHP code

if(isset($_POST['submit'])){

    // use global variable for database opertation
        global $wpdb;
    // assign value that pass from form 
        $fname  = $_POST['fname'];
        $lname= $_POST['lname'];
    // call insert method of wpdb class 
    $wpdb->insert('table_name', array(
        'fname' => $fname,
        'lname ' => $lname // ... and so on
    ));
}

Refer link for more details about database operations in wordpress:

https://codex.wordpress.org/Class_Reference/wpdb

Upvotes: 1

Vasim Shaikh
Vasim Shaikh

Reputation: 4542

Step 1: You have to load wp-configuration if you are use external file structure.

require_once('../../../wp-load.php');

Step 2: Create a function insertuser() with or without argument.

function insertuser(){
//Write code here
}

Step 3: Check is form sumbitted or not if submit then process a variable for database operation purpose.

if(isset($_POST['submit'])){

}

Step 4: If form sumbitted the use $wpdp object for database operation. You can find more details here

global $wpdb; // Global database variable 

https://codex.wordpress.org/Class_Reference/wpdb

Step 5 : get value from $_POST and initalize into a variable and run function for insert.

  $fname=$_POST['fname'];
  $lname=$_POST['lname'];;
  $table_name = $wpdb->prefix . "newsletter";
  $wpdb->insert($table_name, array('lname' => $lname, 'lname' => $lname) ); 

Complete Source Code

require_once('../../../wp-load.php');


function insertuser(){

 if(){
  global $wpdb;
  $fname=$_POST['fname'];
  $lname=$_POST['lname'];;
  $table_name = $wpdb->prefix . "newsletter";
  $wpdb->insert($table_name, array('lname' => $lname, 'lname' => $lname) ); 

  }
  ?>
   <form action="" method="post">
    Firstname: <input type="text" name="fname" /><br><br>
    Lastname: <input type="text" name="lname" /><br><br> 
    <input type="submit" name="submit"/>
  </form>

  <?php

}

insertuser();

Upvotes: 2

user5288114
user5288114

Reputation:

Okay, your questions explains how new you are..!!

No worries, let me tell you some thing you can insert your data using PHP in same page or can also add a new page. The HTML form action attribute decides where the data will be sent and if it is blank it will be submitted to the same page.

You just try submitting to the same page first you get your grip and to insert data at MySQL you need to connect with MySQL first and insert query.

LIKE:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

Above code taken from - $servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

Above code taken from - http://www.w3schools.com/php/php_mysql_insert.asp

Thanks

Upvotes: 1

Related Questions