首页 > 解决方案 > 使用 php 从 html 表单插入 sql 表

问题描述

我正在尝试在我的 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"; ?>

标签: phpmysqlmysql-workbench

解决方案


我失踪了

$statement->execute();

以上

$connection->exec($statement);

推荐阅读