skip to Main Content

I have created a custom form using HTML in a WordPress site and I needed to insert the form data into my custom db ( Wamp Server using phpmyadmin). Where should I place the sqli queries for the same so that the data gets appended to my table in my db when Submit button is clicked. I’m running a local WordPress site.

<html>
<head>
<title>insert data in database using mysqli</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div id="main">
<h1>Insert data into database using mysqli</h1>
<div id="login">
<h2>New Client</h2>
<hr/>
<form action="" method="post">
<label>Client Name  :</label>
<input type="text" name="Name" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
<label>Aadhar  :</label>
<input type="text" name="Aadhar" id="Aadhar" required="required"  placeholder="Please enter Aadhar Number"/><br/><br />
<label>Mobile  :</label>
<input type="text" name="Mobile" id="Mobile" required="required"  placeholder="Please Enter Mobile Number"/><br/><br />
<label>Company  :</label>
<input type="text" name="Company" id="Company"  placeholder="Please Enter Company"/><br/><br />
<label>Description  :</label>
<input type="text" name="Description" id="Description"  placeholder="Please Enter Description"/><br/><br />

<input type="submit" value=" Submit " name="submit"/><br />
</form>
</div>
<!-- Right side div -->


</div>

</body>
</html> 

2

Answers


  1. on click of the submit button you can write the ajax call which can call a function written in the your themes functions.php file. where you can write a code to insert the data in your custom table. i recommend you to create a child theme so when you update the wordpress your changes will not reverted. tutorials for creating child theme also for the ajax call you can refer to the below links

    https://wordpress.stackexchange.com/questions/310213/how-to-call-a-php-function-from-javascript-in-wordpress

    https://wordpress.stackexchange.com/questions/242278/wordpress-ajax-url-for-function-in-functions-php

    Login or Signup to reply.
  2. Add the following Code on your theme function.php file.

    function themename_add_new_data() {
    
          $name         = $_POST['Name'];
          $adhar  = $_POST['Aadhar'];  
          $mobile  = $_POST['Mobile'];  
          $company  = $_POST['Company'];  
          $description  = $_POST['Description'];  
    
          global $wpdb; 
          $table_name = $wpdb->prefix . "testtable"; 
          $wpdb->insert($table_name, array(
                                    'name' => $name, 
                                    'adhar' => $adhar,
                                    'mobile' => $mobile,
                                    'company' => $company,
                                    'description' => $description
                                    ),array(
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',) 
            );
          }
    
    
    
    if( isset($_POST['submit']) ) themename_add_new_data();
    

    I have tested the form and function its working for me

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search