添加到MySQL数据库时,PHP代码具有未知错误

时间:2018-12-04 00:17:15

标签: php mysql pdo

我正在尝试添加到MySQL数据库中,该数据库保留一个有关停车位的用户评论表。当我运行页面时,它不会重定向到下一页,也不会更新表,这表明我做错了事,但是我不知道该怎么办。

PDO:

   if($_SERVER["REQUEST_METHOD"] == "POST"){

    //prepare the variables
    $name = trim($_POST["name"]);
    $review = trim($_POST["review"]);
    $value = $_POST["Rating"];

    //prepare sql statement
    $sql = "INSERT INTO reviews (pid, value, review, name) VALUES (1, :value, :review, :name)";

    if($stmt = $pdo->prepare($sql)){
       // Bind variables to the prepared statement as parameters
       $stmt->bindParam(":name", $param_name, PDO::PARAM_STR);
       $stmt->bindParam(":review", $param_review, PDO::PARAM_STR);
       $stmt->bindParam(":value", $param_value, PDO::PARAM_INT);

       // Set parameters
       $param_name = $name;
       $param_review = $review;
       $param_value = $value;

       // Attempt to execute the prepared statement
       if($stmt->execute()){
          // Redirect back to submission
          header("location: submission.php");
            }
...

这是评论的模式:

+--------+----------------------+------+-----+---------+----------------+
| Field  | Type                 | Null | Key | Default | Extra          |
+--------+----------------------+------+-----+---------+----------------+
| id     | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| p_id   | smallint(5) unsigned | NO   | MUL | NULL    |                |
| value  | smallint(5) unsigned | NO   |     | NULL    |                |
| review | varchar(1000)        | YES  |     | NULL    |                |
| name   | varchar(20)          | NO   |     | NULL    |                |
+--------+----------------------+------+-----+---------+----------------+

其中p_id是另一个表的外键。

我已连接到数据库,并且这组命令适用于其他页面以及没有外键的测试表,我认为p_id是问题所在。我该怎么做才能解决此问题?

0 个答案:

没有答案