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.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘How to insert data using PHP/WordPress?’ is closed to new replies.