2015-04-12 53 views
-2

我試過這個PHP代碼,出現很多錯誤,我無法識別 哪裏是我的初學者錯誤,我真的很希望你的幫助儘快: (未定義索引+沒有選擇數據庫

錯誤:未定義指數::名稱在C:\ XAMPP \ htdocs中\ phpfile.php上線23

說明:未定義指數: /*成功

通知電子郵件連接用C :\ xampp \ htdocs \ phpfile.php on line 24

注意:未定義指數:電話在C:\ XAMPP \ htdocs中\ phpfile.php上線25

說明:未定義指數:口令在C:\ XAMPP \ htdocs中\ phpfile.php上線26 錯誤:INSERT INTO用戶(姓名,電子郵件,電話,密碼)VALUES( '', '', '', '') 沒有數據庫中選擇

*/ // HTML表單:

<html> 
<head> 
<title> event reg </title> 
    <link rel="stylesheet" href="style.css"> 
</head> 
<body> 
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> 
<form enctype="from-data" action="phpfile.php" method="post"> 
      <label for ="name"> User Name :</label> 
      <input type="text" name="name" id="name" maxlength="30" Size="30"/> 
      <br /> 
      <br /> 
      <label for ="email"> E-mail &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;: </label> 
      <input type="email" name="email" id="email" maxlength="255" Size="30"/> 
      <br /> 
      <br /> 
      <label for ="phone"> Phone &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; :</label> 
      <input type="text" name="phone" id="phone" maxlength="10" Size="30"/> 
      <br /> 
      <br /> 
      <label for ="password"> password &nbsp; &nbsp;: </label> 
      <input type="password" name="password" id="password" maxlength="12" Size="30"/> 
      <br /> 
      <br /> 

      <label for ="event"> Event name:&nbsp;&nbsp; </label> 
      <select name="event" id="event"> 
      <option value="Inceptionopening"> Inception </option> 
      </select> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 

      <input type="submit" name="submit" id="submit" value="submit" /> 
      <input type="reset" name="reset" id="reset" value="Cnacel" /> 
</form> 
</body> 
</html> 

//連接到它的PHP代碼:

<?php 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "userdb"; 

    // Create connection 
    $conn = mysqli_connect($servername, $dbname); 

    // Check connection 
    if (!$conn) { 
     die("Connection failed: " . mysqli_connect_error()); 
    } 

    echo "Connected Successfully <br>"; 

    $name=$_POST['name']; 
    $email=$_POST['email']; 
    $phone=$_POST['phone']; 
    $password=$_POST['password']; 
    $sql="INSERT INTO user(name,email,phone,password)VALUES('$name','$email','$phone','$password')"; 

    if (mysqli_query($conn, $sql)) { 
     echo "New record created successfully"; 
    } else { 
     echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
    } 

    mysqli_close($conn); 

    ?> 
+0

你應該檢查'$ _POST'中有你試圖使用它之前預計的數據。另外,看起來好像你正在連接到MySQL正確 - 試試'$ conn = mysqli_connect($ servername,$ username,$ password,$ dbname);' –

+0

我連接到一個html格式的PHP我試過填寫表格並顯示此錯誤。我也寫了$ conn = mysqli_connect($ servername,$ username,$ password,$ dbname);它也會顯示相同的錯誤:\ –

+0

'var_dump($ _ POST)' - 查看發送到您的表單的內容。此外,由於您尚未共享您的表單的代碼,我認爲假設您有'method =「get」'或未指定,而不是使用'method =「post」'可能是安全的? –

回答

0

嘗試這個

<?php 
     $servername = "localhost"; 
     $username = "root"; 
     $password = ""; 
     $dbname = "userdb"; 

    $con = mysqli_connect($servername,$username,$password,$dbname); 
+0

我試過它沒有任何區別/: –

+0

這只是這裏的一個問題 –