2013-05-29 26 views
-1

我嘗試這個在3級不同的操作系統。我正在使用XAMPP,並且使用所有3種操作系統(Mac,Windows,Linux),我在同一個網絡上遇到同樣的問題。POST方法不工作的多個操作系統

形式操作方法是register.php,和形式,它是自我index.html

的錯誤是:

Notice: Undefined index: username in D:\xampp\htdocs\register.php on line 2 

Notice: Undefined index: password in D:\xampp\htdocs\register.php on line 3 

Notice: Undefined index: email in D:\xampp\htdocs\register.php on line 4 

Notice: Undefined index: fname in D:\xampp\htdocs\register.php on line 5 

Notice: Undefined index: lname in D:\xampp\htdocs\register.php on line 6 

的index.html代碼:

<html> 
<h1> Register for Home Server </h1> 
<br> 
<form method="post" action="register.php"> 
Username: <input type="text" id="username"> 
<br> 
Password: <input type="password" id="password"> 
<br> 
First Name: <input type="text" id="fname"> 
<br> 
Last Name: <input type="text" id="lname"> 
<br> 
Email: <input type="text" id="email"> 
<br> 
<input type="submit" value="Register"> 
</form> 
</html> 

register.php代碼:

<?php 
    $user = $_POST['username']; 
    $pass = $_POST['password']; 
    $email = $_POST['email']; 
    $fnam = $_POST['fname']; 
    $lnam = $_POST['lname']; 
    $id = 1; 
    $level = 1; 
    $exp = 100; 

    $con = mysqli_connect('localhost','root','','members'); 
    $sql = 'INSERT members(id,username,password,fname,lname,email,level,exp) VALUES          ($id,$user,$pass,$email,$fnam,$lnam,$email,$level,$exp);'; 
    mysqli_query($con,$sql); 
?> 

沒有MySQL錯誤,最大後大小8MB,而我自己也嘗試增加,

var_dump($_POST); 
die(); 

我目前運行的是Windows 8

+0

糾正你的SQL查詢與添加「或「用於插入串 – Vineet1982

+0

可能重複的標誌[PHP:‘注意:未定義變量’和‘通知:未定義的索引’](HTTP://計算器。com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jocelyn

回答

1

你沒有name屬性。

<html> 
<h1> Register for Home Server </h1> 
<br> 
<form method="post" action="register.php"> 
Username: <input type="text" name ='username' id="username"> 
<br> 
Password: <input type="password" name='password' id="password"> 
<br> 
First Name: <input type="text" name='fname' id="fname"> 
<br> 
Last Name: <input type="text" name='lname' id="lname"> 
<br> 
Email: <input type="text" name='email' id="email"> 
<br> 
<input type="submit" value="Register"> 
</form> 
</html> 

name屬性值是成爲表單提交索引的值。

1

你的HTML輸入字段缺少name屬性。爲了使瀏覽器以POST或GET形式發送數據,必須設置name屬性。

例HTML

<input type="password" name="password" id="password"> 

例PHP

<?php echo $_POST['password']; ?> 

不管你在DOM中name屬性設置爲將被在PHP接收的POST陣列的key側。

1

你必須給你的文本框name屬性:

<form method="post" action="register.php"> 
Username: <input type="text" id="username" name="username"> 
<br> 
Password: <input type="password" id="password" name="password"> 
<br> 
First Name: <input type="text" id="fname" name="fname"> 
<br> 
Last Name: <input type="text" id="lname" name="lname"> 
<br> 
Email: <input type="text" id="email" name="email"> 
<br> 
<input type="submit" value="Register"> 
</form> 

順便說一句,這是檢查是否值已被髮送到PHP腳本是一個好主意(使用isset($_POST['username']))在嘗試使用這些值之前。這也是使用prepared SQL statement防止SQL injection一個好主意。

+0

非常感謝,我對html不太好,儘管我知道所有的web服務器端語言,我認爲id定義了這些名字: P .. –

-2

有兩個錯誤首先提供字段名的變量名稱也:

<input type="text" id="email" name='email'> 

和第二是改變你的SQL你,然後嘗試爲

$sql = "INSERT members(id,username,password,fname,lname,email,level,exp) 
    VALUES ('$id', '$user', '$pass', '$email', '$fnam', '$lnam', '$email', 
    '$level','$exp');";