Insert data form php mysql




For insert data in MySQL first we have to create a table in data base.

Here we using 3 file for insert data in MySQL:

  • database.php:For connecting data base
  • insert.php:for getting the values from the user
  • process.php:A PHP file that process the request

CREATE TABLE `employee` (
	`userid` int(8) NOT NULL,
	`first_name` varchar(55) NOT NULL,
	`last_name` varchar(55) NOT NULL,
	`city_name` varchar(55) NOT NULL,
	`email` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

database.php

insert.php



  
	
First name:

Last name:

City name:

Email Id:


process.php

" . mysqli_error($conn);
	 }
	 mysqli_close($conn);
}
?>

Alternative Code in (PDO)

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $sql = "INSERT INTO employee (first_name,last_name,city_name,email,datetime)
    VALUES ('$first_name', '$last_name','$city_name','$email','$insertdate')";
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
echo $sql . "
" . $e->getMessage(); }
$conn = null; ?>




Data can be entered into MySQL tables by executing SQL INSERT statement through PHP function mysql_query. Below a simple example to insert a record into employee table.

Example

Try out following example to insert record into employee table.

In real application, all the values will be taken using HTML form and then those values will be captured using PHP script and finally they will be inserted into MySQL tables.

While doing data insert its best practice to use function get_magic_quotes_gpc() to check if current configuration for magic quote is set or not. If this function returns false then use function addslashes() to add slashes before quotes.

Example

Try out this example by putting this code into add_employee.php, this will take input using HTML Form and then it will create records into database.


   
   
      Add New Record in MySQL Database
   
   
   
      
            
               
Employee Name
Employee Address
Employee Salary
   
 

php_and_mysql.htm

How can we store form data in MySQL database using PHP?

XAMPP Server (Web Server) HTML. PHP. MySQL..
Start XAMPP Server..
Open localhost/phpmyadmin in your web browser..
Create database of name staff and table of name college..
Write HTML and PHP code in your Notepad in a particular folder..
Submit data through HTML Form..
Verify the results..

How send data from HTML form to database in PHP?

For this you need to follow the following steps:.
Step 1: Filter your HTML form requirements for your contact us web page. ... .
Step 2: Create a database and a table in MySQL. ... .
Step 3: Create HTML form for connecting to database. ... .
Step 4: Create a PHP page to save data from HTML form to your MySQL database. ... .
Step 5: All done!.

How do you insert data into MySQL?

To insert data into a MySQL table, you would need to use the SQL INSERT INTO command. You can insert data into the MySQL table by using the mysql> prompt or by using any script like PHP.

How add data with submit button in PHP?

Insert Data Into a Database using HTML form "Submit. php" file connects to a database, and PHP $ _POST variable receives the value from the form. Then, mysqli_query() function executes INSERT INTO statement, and a new record will be added to the "contact" table. submit.