【问题标题】:Insert into sql table with php from html form使用 php 从 html 表单插入 sql 表
【发布时间】:2020-03-25 21:30:33
【问题描述】:

我正在尝试在我的 sql 数据库中插入我的表(课程)。但是当我运行我的代码(通过单击提交)时,我收到了这个错误:

我不再收到错误消息,我收到消息:

新课程创建成功

但是当我查看数据库时,没有添加课程

这是我的代码:

<?php

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

        require "../config.php";
        require "../common.php";
        $connection = new PDO($dsn, $username, $password);
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO course (courseName, cDescription, programID, programYear, credit) 
                VALUES (:courseName, :cDescription, :programID, :programYear, :credit)";

        $courseName = $_POST['courseName'];
        $cDescription = $_POST['cDescription'];
        $programID = $_POST['programID'];
        $programYear = $_POST['programYear'];
        $credit = $_POST['credit'];

        $statement = $connection->prepare($sql);

        $statement->bindParam(':courseName', $courseName, PDO::PARAM_STR);
        $statement->bindParam(':cDescription', $cDescription, PDO::PARAM_STR);
        $statement->bindParam(':programID', $programID, PDO::PARAM_STR);
        $statement->bindParam(':programYear', $programYear, PDO::PARAM_STR);
        $statement->bindParam(':credit', $credit, PDO::PARAM_STR);

        $connection->exec($statement);

        echo "New course created successfully";


    } catch(PDOException $error) {
        echo $statement. "<br>" . $error->getMessage();
    }
}

?>

<?php include "templates/header.php"; ?>

<h2>Add a course</h2>

    <form method="post">
            <label for="courseName">Course Name:</label>
            <input type="text" name="courseName" id="courseName" required>
            <label for="cDescription">Course Description:</label>
            <input type="text" name="cDescription" id="cDescription" size="40" required>
            <label for="programID">Program ID:</label>
            <input type="number" name="programID" id="programID" required>
            <label for="programYear">Program Year:</label>
            <input type="number" name="programYear" id="programYear" required>
            <label for="credit">credit:</label>
            <input type="number" name="credit" id="credit" required>

            <input type="submit" name="submit" value="Submit">
    </form>

    <a href="index.php">Back to home</a>

    <?php include "templates/footer.php"; ?>

为了尝试看看出了什么问题,我尝试将其简化为可行

<?php

if (isset($_POST['submit'])) {
  $servername = "localhost";
  $username = "username";
  $password = "password";
  $dbname = "courseselector";

  try {
      $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      // set the PDO error mode to exception
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql = "INSERT INTO course (courseName, cDescription, programID, programYear, credit) 
      VALUES ('courseName', 'cDescription', 1, 4, 1)";
      // use exec() because no results are returned
      $conn->exec($sql);
      echo "New record created successfully";
      }
  catch(PDOException $e)
      {
      echo $sql . "<br>" . $e->getMessage();
      }

  $conn = null;

}

?>

<?php include "templates/header.php"; ?>

<h2>Add a course</h2>

    <form method="post">          
            <input type="submit" name="submit" value="Submit">
    </form>

    <a href="index.php">Back to home</a>

    <?php include "templates/footer.php"; ?>

【问题讨论】:

  • 你的代码是报告"New record created successfully"还是给出错误信息?如果是后者,是哪条消息?
  • 你可以用PDO::errorInfo检查错误。
  • 谢谢@KIKOSoftware 我找到了我没有得到“新记录创建成功”的原因,我的输入中没有 name="submit",我添加了我得到的错误现在。
  • 您忘记了 VALUES 中占位符的 :。
  • 如错误信息所述,您的程序 ID 1 不存在于表程序中

标签: php mysql mysql-workbench


【解决方案1】:

我不见了

$statement->execute();

以上

$connection->exec($statement);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多