错误:无法执行INSERT INTO信息(名称,信息)VALUES(:name,:info)。 SQLSTATE [23000]

时间:2018-06-03 07:55:21

标签: php mysql

  

错误:无法执行INSERT INTO信息(名称,信息)   VALUES(:name,:info)。 SQLSTATE [23000]:完整性约束   违规行为:1048栏'名称'不能为空

获取此错误,我不知道如何处理它。这些东西真的很初学,需要真正简化和解释这个问题的答案。我为这个问题找到的所有答案都太复杂了。

<?php

try{
  $pdo = new PDO("mysql:host=3adad;dbname=efvcavc", "admin", "");
  // Set the PDO error mode to exception
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
  die("ERROR: Could not connect. " . $e->getMessage());
}

// Attempt insert query execution
try{
  // create prepared statement
  $sql = "INSERT INTO information (name, info) VALUES (:name, :info)";
  $stmt = $pdo->prepare($sql);

  // bind parameters to statement
  $stmt->bindParam(':name', $_REQUEST['name']);
  $stmt->bindParam(':info', $_REQUEST['info']);


  // execute the prepared statement
  $stmt->execute();
  echo "Records inserted successfully.";
} catch(PDOException $e){
  die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}

// Close connection
unset($pdo);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Record Form</title>
</head>
<body>
<form action="insert.php" method="post">
    <p>
        <label for="firstName">Nimesi:</label>
        <input type="text" name="name" id="Name">
    </p>
    <p>
        <label for="lastName">Ajankohtaista päivitys</label>
        <input type="text" name="info" id="info">
    </p>

    <input type="submit" value="Submit">
</form>
</body>
</html>

0 个答案:

没有答案