Skip to content

Instantly share code, notes, and snippets.

@nayanchy
Created July 11, 2019 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nayanchy/2a0da8b98f0db0a020b4a406dd09313e to your computer and use it in GitHub Desktop.
Save nayanchy/2a0da8b98f0db0a020b4a406dd09313e to your computer and use it in GitHub Desktop.
Simple form to insert into Database using PDO
<?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