PHP: Add new Record to MySQL in wordpress CMS

1. To save record in table in MySQL in wordpress ,first of all configure your “wp-config.php”

php6

  • Edit above file
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'root');

/** MySQL database password */
define('DB_PASSWORD', '');

/** MySQL hostname */
define('DB_HOST', '127.0.0.1');

2.Install plugin for PHP code execution

php3

 

3. Activate the plugin and open to write PHP code

php4

4. Click on “Shortcode Exec PHP”

php7

  •  Complete code is here
if( isset($_POST["submitButton"]) )
  {
    	$c= $_POST["txt1"];
		$host = '127.0.0.1';
		$user = 'root';
		$pass = '';
		$dbname = 'wordpress';
		$db= new  mysqli($host,$user,$pass,$dbname);
   $sql=" insert  into  employee(ename)  values('$c')";
    $result = $db->query($sql);
	 if( $result!=0)
	 {
		 echo "Inserted Sucessfully";
	 }
	 	 else
	 {	 echo "......not Inserted ";
	 }
	 $db->close();
 }		
 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
echo '<html xmlns="http://www.w3.org/1999/xhtml">';
echo '<head>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
echo '<title>Untitled Document</title>';
echo '</head>';
echo '<body>';
echo '<h1>  Add New Employee  </h1>';
echo '<form action="http://localhost/wordpress/?page_id=40"  method="POST">  Enter   Name <input type="text" name="txt1"/> <br/> <br/><input type="submit" name="submitButton" value="Save" /> </form>';

echo '</body>';
echo '</html>';

5.Add New Page use [saveCode]  from Shortcode Exec PHP plugin

php5

6. run the application

php1

 

7. Click the save button..

php2

 

8. Open MySQL and see the save record ..

php8

 

Posted in PHP