将 PHP 表单连接到 MySQL 问题
Connecting PHP form to MySQL issues
我正在尝试在这里学习新事物,但我从未与 PHP 或 MySQL 合作过。最近,我在我拥有的 Linux 虚拟机上安装了 MySQL,创建了一些帐户,创建了一个数据库,一个 table(名字、姓氏、城市、国家、年龄)。
学习了如何将数据输入我的 table 并开始使用 MySQL 查询插入内容。我很快意识到以这种方式输入所有这些数据是多么低效和耗时。所以我开始研究并决定在我拥有的同一网络内的 Windows 10 台机器上的 IIS 之上安装 PHP。
让我的 SQL 数据库可以从我的 Windows 机器远程访问,我开始按照这个 guide 创建我的 website/forms 并将所有内容连接到 MySQL 数据库我已经有了。
一切都很顺利,直到我开始向 create.php 文件添加代码以尝试在单击提交按钮后将数据插入数据库中。
下面是我的 create.php
现在的样子:
<?php
if (isseet($_POST['submit'])) {
require "../config.php";
try {
$connection = new PDO($dsn, $username, $password, $options);
// insert new user code will go here
$new_user = array(
"firstname" => $_POST['firstname'],
"lastname" => $_POST['lastname'],
"city" => $_POST['city'],
"country" => $_POST['country'],
"age" => $_POST['age']
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"users",
implode(", ", array_keys($new_user)),
":" . implode(", :", array_keys($new_user))
);
$statement = $connection->prepare($sql);
$statement->execute($new_user);
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php include "templates/header.php"; ?><h2>Add a user</h2>
<form method="post">
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname">
<label for="lastname">Last Name</label>
<input type="text" name="lastname" id="lastname">
<label for="city">City</label>
<input type="text" name="city" id="city">
<label for="country">Country</label>
<input type="text" name="country" id="country">
<label for="age">Age</label>
<input type="text" name="age" id="age">
<input type="submit" name="submit" value="Submit">
</form>
<a href="index.php">Back to home</a>
<?php include "templates/footer.php"; ?>
每次保存并重新加载 http://localhost/create.php
时,我都会收到以下错误:
PHP Fatal error: Uncaught Error: Call to undefined function isseet() in C:\inetpub\wwwroot\create.php:3
Stack trace:
#0 {main}
thrown in `C:\inetpub\wwwroot\create.php on line 3`
同样,我对此很陌生,我正在尽力理解这一点。
您的第一行有错字。应该是:
if (isset($_POST['submit']))
你在那里有一个额外的'e'。
我正在尝试在这里学习新事物,但我从未与 PHP 或 MySQL 合作过。最近,我在我拥有的 Linux 虚拟机上安装了 MySQL,创建了一些帐户,创建了一个数据库,一个 table(名字、姓氏、城市、国家、年龄)。
学习了如何将数据输入我的 table 并开始使用 MySQL 查询插入内容。我很快意识到以这种方式输入所有这些数据是多么低效和耗时。所以我开始研究并决定在我拥有的同一网络内的 Windows 10 台机器上的 IIS 之上安装 PHP。
让我的 SQL 数据库可以从我的 Windows 机器远程访问,我开始按照这个 guide 创建我的 website/forms 并将所有内容连接到 MySQL 数据库我已经有了。
一切都很顺利,直到我开始向 create.php 文件添加代码以尝试在单击提交按钮后将数据插入数据库中。
下面是我的 create.php
现在的样子:
<?php
if (isseet($_POST['submit'])) {
require "../config.php";
try {
$connection = new PDO($dsn, $username, $password, $options);
// insert new user code will go here
$new_user = array(
"firstname" => $_POST['firstname'],
"lastname" => $_POST['lastname'],
"city" => $_POST['city'],
"country" => $_POST['country'],
"age" => $_POST['age']
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"users",
implode(", ", array_keys($new_user)),
":" . implode(", :", array_keys($new_user))
);
$statement = $connection->prepare($sql);
$statement->execute($new_user);
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php include "templates/header.php"; ?><h2>Add a user</h2>
<form method="post">
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname">
<label for="lastname">Last Name</label>
<input type="text" name="lastname" id="lastname">
<label for="city">City</label>
<input type="text" name="city" id="city">
<label for="country">Country</label>
<input type="text" name="country" id="country">
<label for="age">Age</label>
<input type="text" name="age" id="age">
<input type="submit" name="submit" value="Submit">
</form>
<a href="index.php">Back to home</a>
<?php include "templates/footer.php"; ?>
每次保存并重新加载 http://localhost/create.php
时,我都会收到以下错误:
PHP Fatal error: Uncaught Error: Call to undefined function isseet() in C:\inetpub\wwwroot\create.php:3
Stack trace:
#0 {main}
thrown in `C:\inetpub\wwwroot\create.php on line 3`
同样,我对此很陌生,我正在尽力理解这一点。
您的第一行有错字。应该是:
if (isset($_POST['submit']))
你在那里有一个额外的'e'。