Skip to content

Instantly share code, notes, and snippets.

@howyi
Created July 11, 2015 07:10
Show Gist options
  • Save howyi/4574d86c85b254ca73b1 to your computer and use it in GitHub Desktop.
Save howyi/4574d86c85b254ca73b1 to your computer and use it in GitHub Desktop.
SQL(INSERT INTO) with PDO
<-- PHP 5.6.7 -->
<form action="" method="post">
name: <input type="text" name="name" />
age: <input type="text" name="age" />
<input type="submit"/>
</form>
<?php
$dsn = "mysql:dbname=databese;host=localhost";
$user = "username";
$password = "password";
try{
$pdo = new PDO($dsn, $user, $password);
if($_POST){
$sql = $pdo->prepare("INSERT INTO tabelaname VALUES(0,:name,:age)");
$sql->bindParam(':name',$_POST["name"]);
$sql->bindParam(':age',$_POST["age"]);
$sql->execute();
}
}catch (PDOException $e){
print("error:".$e->getMessage());
die();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment