Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. <?php
  2. //code for adding the child theme inclusion so we don't break anything
  3. ?>
  4. <!--the game begins here-->
  5.  
  6. <!--Self written code of the form submission-->
  7.  
  8. <?php
  9. function add_data_func_table() {
  10. //Fistly we check if the tables exist, if not , constructing it
  11. global $wpdb;
  12. //below set the prefix of wp_ in the table name
  13. $nitk_table= $wpdb->prefix. "nitk_alumni_db";
  14. //this check below checks if the table this exist, if not, the if code is executed
  15. if ($wpdb->get_var("SHOW TABLES LIKE '$nitk_table'") != $nitk_table ) {
  16. //creating the sql table of the alumni database here.
  17. //Now we do charset collation before creation which basically mean adding sql useful stuff such as encoding charset etc in the table. Collate means combining so it combine all the info
  18. $charset = $wpdb->get_charset_collate();
  19. $sql_query = "CREATE TABLE $nitk_table (
  20. id mediumint(9) NOT NULL AUTO_INCREMENT,
  21. name varchar(30) NOT NULL,
  22. year int(4) NULL,
  23. branch varchar(30) NULL,
  24. city varchar(20) NULL,
  25. phone bigint(15) null,
  26. email varchar(320) NOT NULL,
  27. age int(3) NULL,
  28. UNIQUE KEY id (id)
  29. )$charset;";
  30.  
  31. //This reuire statment is reuired for running of the dbDelta function which is stored in upgrade.php file
  32. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  33. //dbDelta checks and create the table by executing sql statement of the above
  34. dbDelta($sql_query);
  35. }
  36. ob_start();
  37. ?>
  38. <!--Now we are making form in which we will take the data and then use wpdb->insert to put it in table-->
  39. <form action="#" method="post" >
  40. <label>Name</label>
  41. <input type="text" name="alum_name">
  42. <label>Year</label>
  43. <input type="text" name="alum_year">
  44. <label>Branch</label>
  45. <input type="text" name="alum_branch">
  46. <label>City</label>
  47. <input type="text" name="alum_city">
  48. <label>Phone</label>
  49. <input type="text" name="alum_phone">
  50. <label>Email</label>
  51. <input type="text" name="alum_email">
  52. <label>Age</label>
  53. <input type="text" name="alum_age">
  54.  
  55. <input type="submit" name="submit_form" value="Submit">
  56. </form>
  57.  
  58. <?php
  59. $html = ob_get_clean();
  60. if (isset($_POST['submit_form'])) {
  61. $nitk_table= $wpdb->prefix. "nitk_alumni_db";
  62. $name = $_POST['alum_name'];
  63. $year = $_POST['alum_year'];
  64. $branch = $_POST['alum_branch'];
  65. $city = $_POST['alum_city'];
  66. $phone = $_POST['alum_phone'];
  67. $email = $_POST['alum_email'];
  68. $age = $_POST['alum_age'];
  69. $wpdb->insert(
  70. $nitk_table,
  71. array(
  72. 'name' => $name,
  73. 'year' => $year,
  74. 'branch' => $branch,
  75. 'city' => $city,
  76. 'phone' => $phone,
  77. 'email' => $email,
  78. 'age' => $age
  79. )
  80. );
  81. $html = "<p>You form has been successfully submitted $name </p>";
  82. }
  83. return $html;
  84. }
  85.  
  86. ?>
  87. <!--Adding shortcode so it can be used directly on page-->
  88. <?php add_shortcode( 'add_data_nitk', 'add_data_func_table' );
  89. ?>
  90. <!--Jane kya hoga rama re-->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement