Created
July 11, 2019 07:20
-
-
Save nayanchy/2a0da8b98f0db0a020b4a406dd09313e to your computer and use it in GitHub Desktop.
Simple form to insert into Database using PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(isset($_POST['submit'])){ | |
try{ | |
$DB = new PDO('mysql:host=localhost; dbname=databasename;', 'user', 'pass'); | |
$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
}catch(PDOException $error){ | |
echo 'Database connection error'; | |
} | |
$stmt = $DB->prepare("INSERT INTO tableName (name, email) VALUES(:name, :email)"); | |
$stmt -> bindParam(":name", $_POST["name"], PDO::PARAM_STR); | |
$stmt -> bindParam(":email", $_POST["email"], PDO::PARAM_STR); | |
$stmt->execute(); | |
} | |
?> | |
<div class="container"> | |
<form method="post" action="index.php"> | |
<input type="text" name="name" placeholder="Name Here"> | |
<input type="email" name="email" placeholder="email Here"> | |
<input type="submit" name="submit"> | |
</form> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment