how to insert data to db in php code example

Example 1: insert a row from databse using php

<?php 
	session_start();
	$db = mysqli_connect('localhost', 'root', '', 'crud');

	// initialize variables
	$name = "";
	$address = "";
	$id = 0;
	$update = false;

	if (isset($_POST['save'])) {
		$name = $_POST['name'];
		$address = $_POST['address'];

		mysqli_query($db, "INSERT INTO info (name, address) VALUES ('$name', '$address')"); 
		$_SESSION['message'] = "Address saved"; 
		header('location: index.php');
	}

// ...

Example 2: how to insert data in mysql using oop php

--  
 -- Table structure for table `tbl_posts`  
 --  
 CREATE TABLE IF NOT EXISTS `tbl_posts` (  
  `post_id` int(11) NOT NULL AUTO_INCREMENT,  
  `post_title` varchar(150) NOT NULL,  
  `post_desc` text NOT NULL,  
  PRIMARY KEY (`post_id`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;  
 --  
 -- Dumping data for table `tbl_posts`  
 --

Tags:

Php Example