Answers for "insert data to database on php form submission"

1

insert form value into database php

/* PDO Class to connect to the database*/
/* Make sure you add the connect file which has the variable $connect */
$name 	  = $_POST["name"];
$password = $_POST["password"];
$stmt = $connect->prepare("INSERT INTO tablename(name, password) VALUES(:name, :pass)");
$stmt->execute(array(
  "name" => $name,
  "pass" => $password
));
/* 
  Check how to connect to the db using PDO 
  Type in google search: php how to connect to db using PDO
  you will find the answer for this question posted by me
*/
Posted by: Guest on June-14-2021
1

HTML form, add the data into it and submit it to the database by connecting it to MySQL database using PHP.

<?php
// database connection code
// $con = mysqli_connect('localhost', 'database_user', 'database_password','database');

$con = mysqli_connect('localhost', 'root', '','db_connect');

// get the post records
$txtName = $_POST['txtName'];
$txtEmail = $_POST['txtEmail'];
$txtPhone = $_POST['txtPhone'];
$txtMessage = $_POST['txtMessage'];

// database insert SQL code
$sql = "INSERT INTO `tbl_contact` (`Id`, `fldName`, `fldEmail`, `fldPhone`, `fldMessage`) VALUES ('0', '$txtName', '$txtEmail', '$txtPhone', '$txtMessage')";

// insert in database 
$rs = mysqli_query($con, $sql);

if($rs)
{
	echo "Contact Records Inserted";
}

?>
Posted by: Guest on October-06-2020

Code answers related to "insert data to database on php form submission"

Browse Popular Code Answers by Language