我对 PHP 非常陌生,并尝试使用 PDO 而不是 mysqli。但是,当我单击提交时,我无法将名字和姓氏的值输入 sql 数据库。我错过了什么?
<!DOCTYPE HTML>
<?php
$server = 'localhost';
$user = 'xxxx';
$pass = 'xxxxx';
$db = 'xxxxxx';
try {
$con = new PDO("mysql:host=$server;dbname=$db",$user,$pass);
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
<html>
<form name="Contact form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
First name: <input type="text" name="firstname"><br />
Last name: <input type="text" name="lastname"><br />
<input type="submit" value="Submit this!" name="submit">
</form>
</html>
<?php
if(isset($_POST['submit'])) {
$sql = $con->query("INSERT INTO names (fname,lname) VALUES (:$_POST[firstname],:$_POST[lastname])");
}
?>
<?php $con = null; ?>