2017-11-02 23 views
0

私はフォームといくつかの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>'; 
?> 
+0

どこからPOSTパラメータを取得してクエリに渡しますか? – BRjava

+1

おそらくあなたはPOST経由であなたの 'insert.php'ページを要求していません。 '$ _POST'配列にデータを入れるためにフォームを提出しなければなりません – Phil

+0

@BRjava 24行から27行 – Phil

答えて

-1

編集:ちょうどことを認識しますあなたはissetをチェックする必要があります:) @philのおかげで

このコードを試してください:

<?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=isset($_POST['first'])?$_POST['first']:""; 
$last=isset($_POST['last'])?$_POST['last']:""; 
$email=isset($_POST['email'])?$_POST['email']:""; 
$message=isset($_POST['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>'; 
?> 
+0

'bindParam'は変数参照にバインドします。バインド時に設定する必要はありません。 – Phil

+0

nope:/同じエラー –

+0

@Phil iの助けを借りてOK:投稿を編集しました:)新しいコードを確認してください – no0ob

関連する問題