私はフォームといくつかのPHPコードをデータベースに情報を入力する必要があります。私は、PHPファイルを実行するとしかし、私はこれらのエラーを取得:ここPHPエラー未定義のインデックス
Notice: Undefined index: first in C:\xampp\htdocs\temp\insert.php on line 24
Notice: Undefined index: last in C:\xampp\htdocs\temp\insert.php on line 25
Notice: Undefined index: email in C:\xampp\htdocs\temp\insert.php on line 26
Notice: Undefined index: message in C:\xampp\htdocs\temp\insert.php on line 27
Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'first' cannot be null in C:\xampp\htdocs\temp\insert.php:29 Stack trace: #0 C:\xampp\htdocs\temp\insert.php(29): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\temp\insert.php on line 29
は私のHTMLフォームです:
<form action="insert.php" method="post">
<label for="first" >Firstname:</label>
<input type="text" name="first" id="first" />
<label for="last" >Surname:</label>
<input type="text" name="last" id="last" />
<label for="email" > Email: </label>
<input type="text" name="email" id="email" />
<label for="message">Message:</label>
<textarea name="message" id="message"> Enter your question here and we will get back
to you as soon as possible </textarea>
<input type="Submit">
</form>
、ここでは私のPHPです:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully <br />";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$query=$conn->prepare("INSERT INTO contacts (first, last, email, message) VALUES(?,?,?,?)");
$query->bindParam(1, $first);
$query->bindParam(2, $last);
$query->bindParam(3, $email);
$query->bindParam(4, $message);
$first=$_POST['first'];
$last=$_POST['last'];
$email=$_POST['email'];
$message=$_POST['message'];
$query->execute();
$conn = null;
echo 'Hi '.$_POST['first'].' ' .$_POST['last'] .' thanks for your interest.</br>';
echo 'We will contact you at '. $_POST['email'].' soon.</br>';
?>
どこからPOSTパラメータを取得してクエリに渡しますか? – BRjava
おそらくあなたはPOST経由であなたの 'insert.php'ページを要求していません。 '$ _POST'配列にデータを入れるためにフォームを提出しなければなりません – Phil
@BRjava 24行から27行 – Phil