2017-03-07 17 views
0

有一個稱爲form.html的頁面。它指向頁面addtodatabase.php 末尾有提交按鈕。如何糾正未定義的索引:第12行中的C: wamp64 www Form addtodatabase.php中的名字錯誤

<form action="addtodatabase.php" method="post"> 
    <form class="form-inline"> 
    <fieldset> 
    <legend>Security Department User Registration</legend> 
    <div class="form-group"> 
    <label for="Firstname">First Name</label> 
    <input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Text input"><br/> 
    </div> 

    <div class="form-group"> 
    <label for="Secondname">Second Name</label> 
    <input type="text" class="form-control" id="Secondname" name="secondname" placeholder="Text input"><br/> 
    </div> 
    </form> 

我的addtodatabase.php頁面。

$connect=mysqli_connect('localhost','root','','form_db'); 
    if(mysqli_connect_errno($connect)) 
    { 
    echo 'Failed to connect:'.mysqli_connect_error(); 
    } 
    $firstname=""; 
    $secondname=""; 
    if (isset($_POST)) { 
    $firstname = isset($_POST['firstname']) ? $_POST['firstname'] : ''; 
    $secondname = isset($_POST['secondname']) ? $_POST['secondname'] : ''; 
    echo 'Your first name is ' .$firstname. '<br>'; 
    echo 'Your second name is ' .$secondname. '<br>'; 
    } 

有三個錯誤

它不會得到addtodatabase.php頁。 http://localhost:8080/form/form.html
注意:未定義的索引:第12行的C:\ wamp64 \ www \ Form \ addtodatabase.php中的名字。 沒有任何內容正在添加到數據庫中。只有id是增量

在此先感謝。

+1

我想你錯過了提交按鈕... – sunilwananje

+1

如果你沒有到達那裏,你如何得到錯誤/通知?爲什麼分配'$ firstname = $ _ POST ['firstname'];'兩次? – chris85

+2

[PHP:「注意:未定義的變量」,「注意:未定義的索引」和「注意:未定義的偏移量」)的可能的重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable- notice_ undefined-index-and-notice-undef) – chris85

回答

0

在您訪問帖子值之前,您最好檢查它是否存在。因此,使用

$firstname = isset($_POST['firstname']) ? $_POST['firstname'] : ''; 
$secondname = isset($_POST['secondname']) ? $_POST['secondname'] : ''; 

更換

$firstname=$_POST['firstname']; 
$secondname=$_POST['secondname']; 

編輯:

$connect=mysqli_connect('localhost','root','','form_db'); 

if(mysqli_connect_errno($connect)) 
{ 
echo 'Failed to connect:'.mysqli_connect_error(); 
} 


$firstname = isset($_POST['firstname']) ? $_POST['firstname'] : ''; 
$secondname = isset($_POST['secondname']) ? $_POST['secondname'] : ''; 

echo 'Your first name is ' .$firstname. '<br>'; 
echo 'Your second name is ' .$secondname. '<br>'; 
} 
+0

我需要在哪裏添加上述更改?上面isset或與if(isset($ _ POST)) –

+0

我編輯它。當名稱設置時,它顯示名稱。如果沒有設置,什麼也不顯示。 –

+0

我已經改變了我的代碼,根據上述代碼我沒有這個未定義的索引錯誤。數據不會被添加form_details不會增加id。 –

0

注 - 在獲取數據前檢查isset。

嘗試這種在同一個文件夾中form.html

$connect=mysqli_connect('localhost','root','','form_db'); 

    if(mysqli_connect_errno($connect)) 
    { 
    echo 'Failed to connect:'.mysqli_connect_error(); 
    } 

    if (isset($_POST)) { 
    $firstname=$_POST['firstname']; 
    $secondname=$_POST['secondname']; 


    echo 'Your first name is ' .$firstname. '<br>'; 
    echo 'Your second name is ' .$secondname. '<br>'; 
    } 
0

是addtodatabase.php?

如果html位於c:\ wamp64 \ www和addtodatabase.php位於C:\ wamp64 \ www \ Form \中,則將html更改爲;

<form action="Form/addtodatabase.php" method="post"> 
<form class="form-inline"> 
<fieldset> 
<legend>Security Department User Registration</legend> 
<div class="form-group"> 
<label for="Firstname">First Name</label> 
<input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Text input"><br/> 
</div> 

<div class="form-group"> 
<label for="Secondname">Second Name</label> 
<input type="text" class="form-control" id="Secondname" name="secondname" placeholder="Text input"><br/> 
</div> 
</form> 
<input type="submit" value="submit"> 
</form> 
+0

你將如何提交此表格? – sunilwananje

+0

奧普斯沒有看到,我已經改變了代碼,謝謝Sunil。 –

相關問題