首页 > 解决方案 > 将 PHP 表单连接到 MySQL 问题

问题描述

我正在尝试在这里学习新事物,但我从未使用过 PHP 或 MySQL。最近,我在我拥有的 Linux VM 上安装了 MySQL,创建了一些帐户,创建了一个数据库,一个表(名字、姓氏、城市、国家、年龄)。

学习了如何将数据输入到我的表中,并开始使用 MySQL 查询插入内容。我很快意识到以这种方式输入所有这些数据是多么的低效和耗时。所以我开始研究并决定在我在同一个网络中拥有的 Windows 10 机器上的 IIS 上安装 PHP。

使我的 SQL 数据库可以从我的 Windows 机器远程访问,我开始按照本指南创建我的网站/表单并将所有内容连接到我已经拥有的 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`

同样,我对此很陌生,我正在尽我所能理解这一点。

标签: phpmysqlundefined-function

解决方案


您的第一行包含一个错字。它应该是:

if (isset($_POST['submit']))

你那里有一个额外的'e'。


推荐阅读