PHP注意:未定义的变量:尝试将数据插入mysql数据库时

时间:2019-11-22 06:04:45

标签: php mysql variables

我有一个MySQL数据库,我正在尝试使用Windows 10计算机上IIS上运行的PHP表单向其中插入数据。 我在.php文件中有以下代码:

<?php

if (isset($_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"; ?>

当我填写表格并点击“提交”以写入mysql数据库时,出现以下错误:

  

PHP注意:未定义的变量:第35行的C:\ inetpub \ wwwroot \ create.php中的sql

我是mysql和php的新手,并试图通过一个小项目来学习基础知识。

3 个答案:

答案 0 :(得分:1)

当您的脚本在try块中有异常时,它将转到catch块,并且没有像$ sql这样的var,因此解决方案是:

$ sql 在try块之前定义一个空变量

答案 1 :(得分:1)

$ sql变量已定义了一个before try块,请检查以下代码-

<?php

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

 require "config.php";
 $sql = "";
 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();
  }
} 
?>

答案 2 :(得分:1)

在这里,您不需要在 catch 块中使用 $ sql 。主要是您必须在 try 块中编辑 $ sql 变量。请找到以下详细信息:

<?php
if(isset($_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("', '", $new_user) . "'"
    );

    $statement = $connection->prepare($sql);
    $statement->execute($new_user);
  } catch(PDOException $error) {
    echo $error->getMessage();
  }
}
?>

在这里,您可以看到变量替换已被修改。这在您的代码中是不正确的,因此将执行catch块,并且发生未定义的变量错误,即与$ sql相关的问题。希望这会有所帮助。