2012-07-17 165 views
-2

我想通過使用xampp在php中製作註冊頁面,但我遇到了問題。當我運行我的PHP文件,它提供了以下錯誤:php註冊系統錯誤

Notice: Undefined index: UserName in C:\xampp\htdocs\register.php on line 3

Notice: Undefined index: UserSurname in C:\xampp\htdocs\register.php on line 4

Notice: Undefined index: UserMail in C:\xampp\htdocs\register.php on line 5

Notice: Undefined index: UserPassword in C:\xampp\htdocs\register.php on line 6

Notice: Undefined index: UserTel in C:\xampp\htdocs\register.php on line 7

Warning: require(class.phpmailer.php) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\register.php on line 24

Fatal error: require() [function.require]: Failed opening required 'class.phpmailer.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\register.php on line 24

我的PHP文件,register.php:

<?php 
$username=$_POST['UserName']; 
$usersurname=$_POST['UserSurname']; 
$usermail=$_POST['UserMail']; 
$userpass=$_POST['UserPassword']; 
$usertelephone=$_POST['UserTel']; 
    $con = mysql_connect("localhost","root","rockenpeace"); 
if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("dbe", $con); 

mysql_query("INSERT INTO user (UserName,UserSurname,UserMail, UserPassword, UserTel) 
     VALUES('$username','$usersurname','$usermail','$userpass','$usertelephone')"); 

mysql_close($con); 

try 
{ 
    require("class.phpmailer.php"); 
    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 
    $mail->Mailer = "smtp"; 
    $mail->Host = "ssl://smtp.gmail.com"; 
    $mail->Port = 465; 
    $mail->SMTPAuth = true; 
    $mail->CharSet="utf-8"; 
    $mail->Username = "[email protected]"; 
    $mail->Password = "password"; 
    $mail->From  = "[email protected]"; 
    $mail->FromName="DBE Yazılım"; 
    $mail->AddAddress($_POST['usermail']); 
    $mail->Subject = "Registration Information"; 
    $mail->Body  = "Hello your password is " . $userpass; 
    //$mail->AddAttachment($path); 
    $mail->Send(); 
    echo 'Message has been sent.'; 
} 
catch(Exception $e) 
{ 
    echo 'hata'.$e->getMessage(); 
} 
header("location:confirmation.php"); ?> 

,並在我的html頁面:

<input type="text" name="UserMail" id="UserMail" size="30" /> 
<input type="text" name="UserName" id="UserName" size="30" /> 
<input type="text" name="UserSurname" id="UserSurname" size="30" /> 
<input type="text" name="UserTel" id="UserTel" size="30" /> 
<input type="password" name="UserPassword" id="UserPassword" size="30" /> 
+0

當我在php中運行print_r($ _ POST)時,它寫入Array()。對不起,我是php.so的初學者,如果我犯了錯誤,警告我。 – rockenpeace 2012-07-17 11:28:53

+0

你剛剛給出了你的smtp信息。我爲你編輯它。 – Martin 2012-07-17 11:29:46

+0

如果它正在寫入Array(),則表單數據沒有傳遞給它。你也應該向我們展示你的標記。 – Martin 2012-07-17 11:31:20

回答

0

好,它看起來像

a)你的$_POST超全球是空的(或至少不包含你認爲它的作用)。這是在這種情況下首先要懷疑的。運行print_r($_POST)以查看內容。

這是不可能的假設 POST/GET變量進入。通過isset()檢查這並檢查它們是否包含你認爲他們應該。簡而言之,我們正在談論驗證。

b)聽起來你的包裝路徑是錯誤的。再次,這很容易檢查。運行echo file_exists('path/to/include') - 如果看不到1,則路徑錯誤

+0

,我沒有看到任何改變。 – rockenpeace 2012-07-17 11:33:56

+0

如果'file_exists(...)'不輸出'1',那麼文件的路徑是錯誤的,正如我說的 – Utkanos 2012-07-17 11:34:22

0

使用isset驗證$ _POST數組中是否存在鍵。

至於你失敗了包括這意味着該文件是不是在你的get_include_path這樣你就可以用set_include_path設置(注意這可能會影響你的其他親戚包括),或者您可以使用absolute path

最後,在您的輸入變量上使用mysql_real_escape_string,否則您非常容易受到SQL injection的影響。

樣品

if (!isset($_POST['userName'], $_POST['key2'], .....)) { 
    // handle this error, you don't have the correct inputs 
} else { 

    // Validate inputs 

    // Escape inputs prior to inserting into database 
    $username = mysql_real_escape_string($_POST['userName']); 
    ... 
    ... 

    // Your queries 
} 
+0

我試圖如果(!isset($ _ POST ['userName'],...)部分和這進入if語句,我已經使用mysql_real_escape_string,但任何東西都不會改變 – rockenpeace 2012-07-17 11:43:32

0

檢查中,其形式操作的頁面是這個頁面,如果在頁面輸入標籤

name屬性是一樣的,你正試圖在這裏找回什麼。

<input type='submit' name='submit' value='register' /> 
</form> 

在register.php

if(isset($_POST['submit'])) 
{ 
    // value in quotes should be same as name attribute in input tag 
    $username=$_POST["username"] 
    .. 
} 

- 更新

<form method ='post' action='register.php' > 
    <input type="text" name="UserMail" id="UserMail" size="30" /> 
    <input type="text" name="fname" id="UserName" size="30" /> 
    <input type="text" name="lname" id="UserSurname" size="30" /> 
    <input type="text" name="telephone" id="UserTel" size="30" /> 
    <input type="password" name="userPassword" id="UserPassword" size="30" /> 
    </form 

,並在註冊頁面

if(isset($_POST['UserMail'])) 
{ 
    $mail =mysql_real_escape_strings($_POST['UserMail']); 
} 
if(isset($_POST['fname'])) 
{ 
    $mail =mysql_real_escape_strings($_POST['fname']); 
} 
if(isset($_POST['lname'])) 
{ 
    $mail =mysql_real_escape_strings($_POST['lname']); 
} 
+0

我已經在我的html頁面中重寫了名稱屬性,但是我仍然有同樣的錯誤 – rockenpeace 2012-07-17 12:21:27

+0

你是否在'

' – 2012-07-17 12:25:14

+0

是的,我已經附上了這個表格:但我的方法部分與你不同 – rockenpeace 2012-07-17 12:31:17

0

在:

<input type="text" name="fname" id="UserName" size="30" /> 

變化name="fname"name="UserName"
對其他領域也一樣。

$_POST使用name attribude,而不是ID


你必須檢查,如果你$_POST變量使用isset()empty()設置。

例與isset()

if(isset($_POST['UserName'])) $username = $_POST['UserName']; 
else die("Username variable is not set."); 

例與empty()

if(!empty($_POST['UserName'])) $username = $_POST['UserName']; 
else die("Username variable is empty."); 

你應該在你心中的另一件事是,你應該使用之前逃脫你的字符串它通過使用進行SQL查詢

例子:

$username = mysql_real_escape_string($_POST['UserName']); 

您可能還需要結合逃脫使用strip_tags()

+0

當我用if(isset($ _ POST ['UserName']] ))$ username = $ _POST ['UserName']; else die(「未設置用戶名變量」);它寫信息。 – rockenpeace 2012-07-17 12:19:27

+0

@rockenpeace這意味着你的變量是空的... – 2012-07-17 12:21:11

+0

你認爲我在我的html頁面中輸入標籤有錯誤嗎?我的輸入標籤: \t \t \t \t \t rockenpeace 2012-07-17 12:28:33