2016-03-29 33 views
-2

問題登記用戶登記表格:我嘗試這樣做,在一個數據庫(MySQL的)登記用戶登記表。
的代碼應該登記: - 用戶名 - 密碼 - 名字 - 姓氏 - 電子郵件我嘗試這樣做,在一個數據庫(MySQL的)

而且我得到這樣的錯誤:

Notice: Undefined index: fname in C:\xampp\htdocs\Railways\reg.php on line 89 

Notice: Undefined index: lname in C:\xampp\htdocs\Railways\reg.php on line 90 

Notice: Undefined index: uname1 in C:\xampp\htdocs\Railways\reg.php on line 91 

Notice: Undefined index: pswrd1 in C:\xampp\htdocs\Railways\reg.php on line 92 

Notice: Undefined index: email in C:\xampp\htdocs\Railways\reg.php on line 93 

Fatal error: Call to a member function query() on null in C:\xampp\htdocs\Railways\reg.php on line 98 

下面是代碼:

<?php 
    global $conn; 
    include_once('connect.php'); 
     $first=$_POST['fname']; 
     $last=$_POST['lname']; 
     $usernam=$_POST['uname1']; 
     $passwrd=$_POST['pswrd1']; 
     $email=$_POST['email']; 
    $sql=" INSERT INTO users(username,password,firstname,lastname,email) VALUES ('".$usernam."','".$passwrd."','".$first."','".$last."','".$email."')"; 
    if($conn->query($sql)) 
    { 
     echo "<h1 style='text-align:center'> Registered Succesfully </h1>"; 
    } 
    else 
    { 
     echo "<h1 style='text-align:center' id='text'>Error</h1>"; 
     echo $conn->connect_error; 
    } 
?> 

這裏是HTML:

<form action="reg.php" method="POST"> 
    <label id="text"> Firstname: <br> <br> <input id="input" type="text" name="fname" placeholder="Firstname" id="t3"> </label> 
    <br> <br> <br> 
    <label id="text"> Lastname: <br> <br> <input id="input" type="text" name="lname" placeholder="Lastname" id="t4"> </label> 
    <br> <br> <br> 

    <label id="text"> Username: <br> <br> <input id="input" type="text" name="uname1" placeholder="Username" id="t1"> </label> 
    <br> <br> <br> 

    <label id="text"> Password: <br> <br> <input id="input" type="text" name="pswrd1" placeholder="Password" id="t2"> </label> 
    <br> <br> <br> 

    <label id="text"> Email: <br> <br> <input id="input" type="text" name="email" placeholder="Email" id="t1"> </label> 
    <br> <br> <br> 

    <input style="background-color:red;padding:7px;margin-left:40px;"type="submit" value="Register" name="submit" id="s1"> 

    </td></tr> 
    </table> 
    </div> 
</form> 

connect.php代碼

<?php 

      $conn_error="could not connect"; 
      $host="localhost"; 
      $user="root"; 
      $pass=""; 
      $db="railways"; 
     $sql =mysqli_connect($host,$user,$pass,$db) or die($conn_error); 
     echo'conneced'; 

?>

+0

份額的代碼; – itzmukeshy7

+0

另外,請參見準備語句 – Strawberry

回答

1

添加一個檢查,如果提交後的設置,那麼,否則運行查詢,如果你打開這個頁面直接$_POST變量是空的,你的理由得到錯誤是你的$_POST數組是空的。

<?php 


include_once('connect.php'); 
global $conn; 
if(isset($_POST['submit'])) // this check 
{ 
    $first=$_POST['fname']; 
    $last=$_POST['lname']; 
    $usernam=$_POST['uname1']; 
    $passwrd=$_POST['pswrd1']; 
    $email=$_POST['email']; 


$sql=" INSERT INTO users(username,password,firstname,lastname,email) VALUES ('".$usernam."','".$passwrd."','".$first."','".$last."','".$email."')"; 

if($conn->query($sql)) 
    { 
    echo "<h1 style='text-align:center'> Registered Succesfully </h1>"; 

    } 
else 
    { 
    echo "<h1 style='text-align:center' id='text'>Error</h1>"; 
    echo $conn->connect_error; 
    } 
} 
?> 
+0

Ok.My錯誤fixed.But我仍然收到此錯誤:致命錯誤:調用一個成員函數查詢()對空在C:\ XAMPP \ htdocs中\鐵路\上線reg.php 99 –

+0

由於這個錯誤,我不能插入註冊信息到我的數據庫 –

+0

編輯我現在答...嘗試移動全球$康恩後的文件包括@賽斯里納 –

0

如果未設置$_POST,則需要將這些值定義爲某些值。

像這樣:connect.php`的`

$first=isset($_POST['fname']) ? $_POST['fname'] : "" ; 
$last=isset($_POST['lname']) ? $_POST['lname'] : ""; 
$usernam=isset($_POST['uname1']) ? $_POST['unmae1'] : ""; 
$passwrd=isset($_POST['pswrd1']) ? $_POST['pswrd1'] : ""; 
$email=isset($_POST['email']) ? $_POST['email'] : ""; 

if(isset($_POST['submit'])){ 
    $sql=" INSERT INTO users(username,password,firstname,lastname,email) VALUES ('".$usernam."','".$passwrd."','".$first."','".$last."','".$email."')"; 

if($conn->query($sql)){ 
    echo "<h1 style='text-align:center'> Registered Succesfully </h1>"; 
} 
else{ 
    echo "<h1 style='text-align:center' id='text'>Error</h1>"; 
    echo $conn->connect_error;} 
} 
+1

:)它有助於阻止錯誤,但我認爲,當打開REG .php註冊他一直在db中添加空行:) –

+0

哦..我錯過了! – user3284463

相關問題