2014-01-19 164 views
-1

我試圖添加更多的字段到我的數據庫註冊表格。我想添加「名字」和「姓氏」,並且已經添加了數據庫中的列以及註冊表單中的字段。但是我不知道在連接到數據庫的實際表單上如何改變(register.inc.php)任何想法,將不勝感激!添加信息到PHP註冊表格

<?php 

include_once 'db_connect.php'; 
include_once 'psl-config.php'; 

$error_msg = ""; 

if (isset($_POST['username'], $_POST['email'], $_POST['p'])) { 
    $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING); 
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); 
    $email = filter_var($email, FILTER_VALIDATE_EMAIL); 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     $error_msg .= '<p class="error">The email address you entered is not valid</p>'; 
    } 

    $password = filter_input(INPUT_POST, 'p', FILTER_SANITIZE_STRING); 
    if (strlen($password) != 128) { 
     $error_msg .= '<p class="error">Invalid password configuration.</p>'; 
    } 


    $prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1"; 
    $stmt = $mysqli->prepare($prep_stmt); 

    if ($stmt) { 
     $stmt->bind_param('s', $email); 
     $stmt->execute(); 
     $stmt->store_result(); 

     if ($stmt->num_rows == 1) { 
      $error_msg .= '<p class="error">A user with this email address already exists.</p>'; 
     } 
    } else { 
     $error_msg .= '<p class="error">Database error</p>'; 
    } 

    if (empty($error_msg)) { 
     $random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE)); 

     $password = hash('sha512', $password . $random_salt); 

     // Insert the new user into the database 
     if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)")) { 
      $insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt); 
      // Execute the prepared query. 
      if (! $insert_stmt->execute()) { 
       header('Location: ../error.php?err=Registration failure: INSERT'); 
       exit(); 
      } 
     } 
     header('Location: ./register_success.php'); 
     exit(); 
    } 
} 
+0

爲什麼你已經添加''在$ insert_stmt' if'條件? –

+0

@KrishR我相信$ mysqli-> prepare可以返回false,儘管你可能想知道它何時返回false。 –

回答

2

你需要:

  1. 更新HTML表單添加新的領域
  2. 更新你的PHP代碼與

    $firstname = filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_STRING); 
    $lastname = filter_input(INPUT_POST, 'lastname', FILTER_SANITIZE_STRING); 
    
  3. 讀取這些值更新MYSQLi準備包括名字和姓氏(注意額外的標籤和?的)

    $insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt, firstname, lastname) VALUES (?, ?, ?, ?, ?, ?)")) 
    
  4. 更新綁定添加第一個名字和姓氏

    $insert_stmt->bind_param('ssssss', $username, $email, $password, $random_salt, $firstname, $lastname); 
    
+0

太棒了,thanx!:) – user3144965

0

我會把它變成一個數組,這樣就節省了我的時間,如果我需要添加更多的列。

<?php 
include_once 'db_connect.php'; 
include_once 'psl-config.php'; 
$error_msg = ""; 
$arr=['username','email','p','firstname','lastname','salt']; 
$arrvalues=new Array(arr.length()) 
for($i=0;$i<$array.length();$i=$i+1){ 
    if (isset($_POST[$arr[$i]]&& !empty($_POST[$arr[$i])) { 
     $arrvalues[$i] = filter_input(INPUT_POST,$arr[$i], FILTER_SANITIZE_STRING); 
     if($arr[$i]=="email"){ 
      $arrvalues[$i]=filter_var($arrvalues[$i], FILTER_VALIDATE_EMAIL); 
      if (!filter_var($arrvalues[$i], FILTER_VALIDATE_EMAIL)) { 
       $error_msg .= '<p class="error">The email address you   entered is not valid</p>'; 
      } 
     } 
     if($arr[$i]=="password"){ 
      if (strlen($arrvalues[$i]) != 128) { 
       $error_msg .= '<p class="error">Invalid password configuration.</p>'; 
      } 
     } 
    } 
} 
$prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1"; 
$stmt = $mysqli->prepare($prep_stmt); 
if($stmt){ 
$stmt->bind_param('s', $arrvalues[1]); 
$stmt->execute(); 
$stmt->store_result(); 
if ($stmt->num_rows == 1) { 
    $error_msg .= '<p class="error">A user with this email address already exists.</p>'; 
} 
} else { 
    $error_msg .= '<p class="error">Database error</p>'; 
} 
if (empty($error_msg)) { 
$arrvalues[5] = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE)); 
$arrvalues[2] = hash('sha512', $arrvalues[2] . $arrvalues[5]); 
// Insert the new user into the database 
for($i=0;$i<$array.length();$i=$i+1){ 
    if ($insert_stmt = $mysqli->prepare("INSERT INTO members (".$arrvalues[$i].") VALUES (?)")) { 
     $insert_stmt->bind_param('ssss', $arrvalues[$i]); 
     // Execute the prepared query. 
     if (! $insert_stmt->execute()) { 
      header('Location: ../error.php?err=Registration failure: INSERT'); 
      exit(); 
     } 
    } 
} 
header('Location: ./register_success.php'); 
exit(); 

}