2016-04-29 57 views
0

註冊人員時出現問題。我已經嘗試登錄,它完美的工作。未將數據插入到XAMPP數據庫中

腳本數據庫

 
CREATE TABLE userinfor (
    userid INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(25) NOT NULL, 
    password VARCHAR(15) NOT NULL, 
    email VARCHAR(35) NOT NULL 
); 

PHP代碼

$emailErr = $passMismatch = ""; 
if(isset($_POST['submit'])){ 
    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     $name = $_REQUEST['name']; 
     $pwd = $_REQUEST['password']; 
     $cpwd = $_REQUEST['cpassword']; 
     $email = $_REQUEST['email']; 

     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
      $emailErr = "ERROR::Invalid E-Mail Format"; 
     } else $emailErr = ""; 

     if ($pwd != $cpwd) { 
      $passMismatch = "ERROR::Passwords Don't Match"; 
     } else $passMismatch = ""; 

     $host = "localhost"; 
     $username = "root"; 
     $password = ""; 
     $database = "ontheway"; 

     $conn = mysqli_connect($host, $username, $password, $database); 

     if (empty($emailErr) && empty($passMismatch)) { 
      $sql = "INSERT INTO userinfo (userid, name, password, email) 
        VALUES (NULL, $name', $pwd', '$email')"; 


      $result = mysqli_query($conn, $sql); 
      if (!$result) { 
       echo "chala nahi query"; 
      } 

      if ($result) { 
       session_start(); 
       $_SESSION['name'] = $name; 
       $_SESSION['pass'] = $pass; 

       header("location: home.php");  
      } 
     } 
    } 
} 

我想呼應的問題,但我無法找到任何,$名稱,$ PWD,$ CPWD,$電子郵件都得到迴應。即使這些錯誤也正在完美地顯示在表格上。數據庫也正在連接。什麼似乎是問題?請幫助:)

我總是得到「chala nahi查詢」,這意味着查詢SQL不會工作,每次我運行這個。

表單代碼

名稱:

      <input type="text" name="name" /> <br /> 

          <p>Password: </p> 
          <input type="password" name="password" /> <br /> 
          <?php echo '<h1>'.$passMismatch.'</h1>'; ?> 

          <p>Confirm Password: </p> 
          <input type="password" name="cpassword" /> <br /> 

          <p>Email: </p> 
          <input type="email" name="email" /> <br /> 
          <?php echo '<h1>'.$emailErr.'</h1>'; ?> 

          <input type="submit" name="submit" /> 

回答

1

周圍的變量還缺少引號無需插入userid,因爲它已經是autoincremented

$sql = "INSERT INTO userinfo (`name`, `password`, `email`) 
        VALUES ('".$name."', '".$pwd."', '".$email."')"; 
+0

耶第一,我沒有插入的用戶ID,但由於它不工作我想也許這可能是問題。隊友的歡呼聲 :) –

1

你缺少圍繞$名稱查詢和$ PWRD變量在查詢 - 它應該是一個單引號;

$sql = "INSERT INTO userinfo (userid, name, password, email) 
        VALUES (NULL, '$name', '$pwd', '$email')"; 

您應該研究PDO和綁定參數 - 更安全。此外 - 你應該消毒輸入,你應該只有一個H1元素在你的頁面上。