2016-07-23 99 views
0

我一直試圖讓我的HTML表單提交到指定的數據庫。我已經嘗試了我所知道的一切,但仍然沒有結果,我似乎無法得到錯誤。我的HTML數據沒有顯示在數據庫中

HTML表單

<form action = "connect.php" method = "post"> 
     <br> 
     <br> 
     <br> 

     <h5 style ="color:White;" style="font-family:verdana" align = "center"><font size = "2"> 

     <fieldset > 
      <legend>Personal information:</legend> 
      First name: <br> 
      <input type = "text" name ="firstname"> 
      <br> 
      Last name: <br> 
      <input type = "text" name ="lastname"><br> 
      Username: <br> 
      <input type = "text" name = "username"><br> 
      Password <br> 
      <input type = "password" name ="password"><br> 
      School <br> 
      <input type = "text" name ="school"><br> 
      Department <br> 
      <input type = "text" name ="department"><br> 
      Year Of Study <br> 
      <input type = "text" name ="studyYear"><br> 
      Email <br> 
      <input type = "text" name ="email"><br> 
      Phone number <br> 
      <input type = "text" name ="phoneNumber"><br><br> 
      Gender <br> 
      <input type = "radio" name = "gender" value ="male">Male<br> 
      <input type = "radio" name = "gender" value ="female">Female<br> 
      <input type = "radio" name = "gender" value ="male">Other<br> 
      <br><br> 
      <input type ="submit" value ="Insert"> 
      </h5> 
     </fieldset> 
    </h5> 
    </form> 

PHP CODE 

<?php 
// Connect to localhost 
$connect = mysqli_connect('localhost', 'root', '', 'sigma'); 

if(!$connect) { 
    echo("Didn\'t work ". mysqli_error()); 
} 

// Get values from the form 
$ID = $_POST['UserID']; 
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$username = $_POST['username']; 
$password = $_POST['password']; 
$school = $_POST['school']; 
$dept = $_POST['department']; 
$studyYear = $_POST['studyYear']; 
$email = $_POST['email']; 
$phone = $_POST['phoneNumber']; 

// SQL query to insert the data collected from the form into the database. 
$sql = mysqli_query ($connect, 'INSERT INTO users (firstname, lastname, username, password, 
    school, department, studyYear, email, phoneNumber) 
VALUES ("'.$firstname.'", "'.$lastname.'", "'.$username.'","'.$password.'", 
"'.$school.'", "'.$dept.'", "'.$studyYear.'", "'.$email.'", '.$phone.')'); 


if ($sql) 
{ 
echo 'Inserted'; 

} 

else 
{ 
echo 'Not Inserted'; 
} 



mysqli_close($connect); 

?> 
+1

請添加您的PHP代碼。 –

+0

你得到了什麼錯誤/錯誤?你確定PHP錯誤已啓用? – VSG24

+0

@ShudhanshShekhar其下面的HTML表單代碼正下方非常感謝你 – Delalixx

回答

1

此查詢應該爲你工作,這幾乎就像Shudhansh,但我用雙引號包裝了INSERT查詢並刪除了t他各變量之間的串連和包裹它們各自在自己的單引號:

$sql = mysqli_query ($connect, 
"INSERT INTO users (firstname, lastname, username, password, 
school, department, studyYear, email, phoneNumber) 
VALUES ('$firstname', '$lastname', '$username', '$password', '$school', '$dept', '$studyYear', '$email', '$phone')"); 

我跑了我的本地機器和它的工作。

另外你的connect.php試圖從請求中獲取一個$ _POST ['userID']',但是你不會從表單傳遞它,這樣就可以將它全部阻止,你應該創建一個auto增加ID作爲處理用戶ID的主鍵。

如果這是以某種方式投入生產,我建議驗證和消毒數據,並使用密碼功能來解決明顯的安全問題。

0

首先檢查您的網絡連接

if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

如果它的確定,檢查結果

if ($result = mysqli_query($connect, $sql)) { 
       echo "I got the result yeah\n", mysqli_num_rows($result); 
      }else{ 
       echo "No results buddy." 
} 
相關問題