首先像这样更改您的连接:$host = 'localhost';
$db = 'nairobi';
$user = 'admin';
$pass = '123456';
$charset = 'utf8mb4'; // Always set charset for database
$port = '3308'; //Your port can be 3306 or 3307
$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
现在输入您的密码!您正在使用prepare语句,这会使您的代码足够安全。if (isset($_POST['submit'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
//You have a `TABLE` name call members so you dont need `TABLE` its causing problem
//Your query should look like this
$sql = 'INSERT INTO members (first_name, last_name, email) VALUES (:first_name, :last_name, :email)';
$stmt = $dbh->prepare($sql);
$stmt->execute([$first_name, $last_name, $email]);
//I removed array here which you dont need, you can directly add fields in execute.
$stmt->closeCursor();
//Use `closeCursor()` to free your connection instead unset or close.
}
最终确保您创建了一个数据库。确保您在数据库中创建了表调用成员。
如果您已完成所有操作,则您的代码将毫无问题地工作。
last_name
Login