Title: How to insert data using PHP/WordPress?
Last modified: October 23, 2017

---

# How to insert data using PHP/WordPress?

 *  [mangesh520](https://wordpress.org/support/users/mangesh520/)
 * (@mangesh520)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/how-to-insert-data-using-php-wordpress/)
 * I have created following WordPress page:
 *     ```
       <div>
        <table>
          <tr>
            <td>
             <label id='lblIdentityNo'> Identity No:</label>
            </td>
            <td>
             <input id='txtIdentityNo' />
            </td>
          </tr>
          <tr>
           <td>
             <label id='lblCustomer'>Customer:</label>
           </td>
           <td>
            <input id='txtCustomer'></input>
           </td>
          </tr>
         <tr>
           <td>
             <label id='lblPartNo'>Part No:</label>
           </td>
           <td>
            <input id='txtPartNo'></input>
           </td>
          </tr>
          <tr>
           <td>
             <label id='lblDescription'>Description:</label>
           </td>
           <td>
            <input id='txtDescription'></input>
           </td>
          </tr>
           <tr>
           <td>
             <label id='lblQuantity'>Quantity:</label>
           </td>
           <td>
            <input id='txtQuantity'></input>
           </td>
          </tr>
          <tr>
           <td>
             <label id='lblNoOfTray'>No. of Tray:</label>
           </td>
           <td>
            <input id='txtNoOfTray'></input>
           </td>
          </tr>
   
        <tr>
           <td>
             <label id='lblCheckDC'>Check DC:</label>
           </td>
           <td>
            <input type='checkbox' id='chkCheckDC'></input>
           </td>
          </tr>
   
       <tr>
           <td>
             <label id='lblWeight'>Weight:</label>
           </td>
           <td>
            <input id='txtWeight'></input>
           </td>
          </tr>
          <tr>
           <td>
             <label id='lblQuantityAndPacking'>Quantity And Packing:</label>
           </td>
           <td>
            <input id='txtQuantityAndPacking'></input>
           </td>
          </tr>
         <tr>
           <td>
             <label id='lblAccepted'>Accepted:</label>
           </td>
           <td>
            <input type='radio' id='rdoYes' name="accepted" value="Yes" checked></input> Yes <input type='radio' id='rdoNo' name="accepted" value="No"></input> No
           </td>
          </tr>
       <tr>
           <td>
             <label id='lblPrewashRequired'>Prewash Required:</label>
           </td>
           <td>
            <input type='radio' id='rdoYes' name="PrewashRequired" value="Yes" checked></input> Yes  <input type='radio' id='rdoNo' name="PrewashRequired" value="No"></input> No
   
           </td>
          </tr>
   
         <tr>
           <td>
             <label id='lblTouleneConsumed'>Toulene Consumed:</label>
           </td>
           <td>
            <input id='txtTouleneConsumed'></input>
           </td>
          </tr>
   
         <tr>
           <td>
             <label id='lblRejected'>Rejected:</label>
           </td>
           <td>
            <input type='checkbox' id='txtRejected'></input>
           </td>
          </tr>
         <tr>
           <td>
             <label id='lblDeliveryChallanDate'>Delivery Challan Date:</label>
           </td>
           <td>
            <input id='txtDeliveryChallanDate'></input>
           </td>
          </tr>
          <tr>
           <td>
             <label id='lblOperator'>Operator:</label>
           </td>
           <td>
            <input id='txtOperator'></input>
           </td>
          </tr>
          <tr  align='center'>   
           <td colspan='2'>
            <input type="submit" name='submit' value="Submit"> <input type="submit" value="Update"> <input type="submit" value="Delete">
           </td>
          </tr>
        </table>
       </div>
   
       Now, I have the following PHP page to insert data into mySql database:
   
       <?php
       /**
        * Template Name: Sample
        *
        * @package WordPress
        * @subpackage Twenty_Sixteen
        * @since Twenty Sixteen 1.0
        */
   
        get_header();?>
   
        <form action="" method=get>
   
       <div id="primary" class="content-area">
       	<main id="main" class="site-main" role="main">
       		<?php
       		// Start the loop.
       		while ( have_posts() ) : the_post();
   
       			// Include the page content template.
       			get_template_part( 'template-parts/content', 'page' );
   
       			// If comments are open or we have at least one comment, load up the comment template.
       			if ( comments_open() || get_comments_number() ) {
       				comments_template();
       			}
   
       			// End of the loop.
       		endwhile;
       		?>
   
       	</main><!-- .site-main -->
   
   
   
       </div><!-- .content-area -->
       </form>
   
   
       <!-- Inserting data into MYSQL table -->
       <?php
   
       	$servername = '************';
       	$username = '************';
       	$password = '************';
       	$dbname = '************';
   
       	// Create connection
       	$conn = new mysqli($servername, $username, $password, $dbname);
       	// Check connection
       	if ($conn->connect_error) {
       		die("Connection failed: " . $conn->connect_error);
       	} 
       	//Get values from form
       	$chkCheckDC =  isset($_POST['chkCheckDC']) ? 1 : 0;
       	$accepted=$_POST['accepted']; 
       	$PrewashRequired=$_POST['PrewashRequired']; 
       	$Rejected =  isset($_POST['Rejected']) ? 1 : 0;
   
       	 $sql = 'INSERT INTO Tag_Inward_Challan(Identity_No, Customer,Part_No,Description,
       	 Quantity, No_Of_Tray,CheckDC,Weight,Quantity_and_Packing,
       	 Accepted,Prewash_Required, Toulene_Consumed,Rejected,Delivery_Challan_Date,Operator)
       	 VALUES (txtIdentityNo.text,txtCustomer.text,txtPartNo.text,txtDescription.text,
       	 txtQuantity.text,txtNoOfTray.text,$chkCheckDC,txtWeight.text,
       	 txtQuantityAndPacking.text,$accepted,$PrewashRequired,txtTouleneConsumed.text,
       	 $Rejected,txtDeliveryChallanDate.text,txtOperator.text
       	 )';	
   
       	if ($conn->query($sql) === TRUE) {
       		echo "New record created successfully";
       	} else {
       		echo "Error: " . $sql . "<br>" . $conn->error;
       	}
   
       	$conn->close();
   
       ?>
       ```
   
 * I want call insert code on submit button click.
    How to insert data using PHP/
   Wordpress?
 * Any help would be appreciated.
    Thanks.
    -  This topic was modified 8 years, 7 months ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).
      Reason: Fixing code block

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [dineshkashera](https://wordpress.org/support/users/dineshkashera/)
 * (@dineshkashera)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/how-to-insert-data-using-php-wordpress/#post-9611390)
 * Hello,
    You don’t need to create connection in wordpress before every query because
   it already done in wp-config.php file at root wordpress folder. you need to use
   wordpress query structure instead to use php core query.
 * we create query using global object $wpdp.
 * To insert a row you must create query by use below code
    Insert two columns in
   a row, the first value being a string and the second a number:
 *     ```
       $wpdb->insert( 
       	'table', 
       	array( 
       		'column1' => 'value1', 
       		'column2' => 123 
       	), 
       	array( 
       		'%s', 
       		'%d' 
       	) 
       );
       ```
   
 * To get more help, please visit this link
    [https://codex.wordpress.org/Class_Reference/wpdb](https://codex.wordpress.org/Class_Reference/wpdb)
 * Thanks
 *  Thread Starter [mangesh520](https://wordpress.org/support/users/mangesh520/)
 * (@mangesh520)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/how-to-insert-data-using-php-wordpress/#post-9612849)
 * I have tried the suggested approach. However, not fruitful. Does anyone have 
   the simple approach to achieve the same?
 * Many thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to insert data using PHP/WordPress?’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [mangesh520](https://wordpress.org/support/users/mangesh520/)
 * Last activity: [8 years, 7 months ago](https://wordpress.org/support/topic/how-to-insert-data-using-php-wordpress/#post-9612849)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
