2012-12-06 43 views
0

我有註冊表單供用戶註冊我的網站。所有這些細節去一個mysql數據庫,如名稱等在註冊表單上生成一個順序的user_id提交

當信息被記錄在數據庫中我想提交的表單,它創建一個順序的user_id並將其與其他細節如姓名和年齡在數據庫中。

我需要什麼代碼?對不起,我是新來的PHP。

我已經嘗試使用這樣的cookie腳本,但它不會創建user_id。而不是在數據庫中的所有用戶都爲0

<?php 
    $user_id_set = get_user_id(); 
    while ($user_id = mysql_fetch_array($user_id_set)) { 
     $cookie1 = "{$user_id["id"]}"; 
     setcookie("ptb_registrations", $cookie1, time()+3600); /* expire in 1 hour */ 

    } 
    ?> 

我的繼承人完整的代碼爲user_id:

<? ob_start(); ?> 
<?php 


// GET ACCOUNT INFORMATION FROM FORM AND ASSIGN VARIABLES 

$first_name = $_POST['first_name']; 
$last_name = $_POST['last_name']; 
$email = $_POST['email']; 
$display_name = $_POST['display_name']; 
$date_of_birth = $_POST['date_of_birth']; 
$contact_number = $_POST['contact_number']; 
$station = $_POST['station']; 
$hobbies = $_POST['hobbies']; 
$age = $_POST['age']; 
$password = $_POST['password']; 
?> 
<?php 
/* 
// ECHO ACCOUNT INFORMATION 
echo "<strong> Account Information: </strong>"; 
echo "<br />"; 
echo First Name: "; 
echo "<br />"; 
echo $first_name; 
echo "<br />"; 
echo "<br />"; 
echo "Last Name: "; 
echo "<br />"; 
echo $last_name; 
echo "<br />"; 
echo "<br />"; 
echo "Email: "; 
echo "<br />"; 
echo $email; 
echo "<br />"; 
echo "<br />"; 
echo "Password: "; 
echo "<br />"; 
echo $password; 
echo "<br />"; 
echo "<br />"; 
echo "date_of_birth: "; 
echo "<br />"; 
echo $date_of_birth; 
echo "<br />"; 
echo "<br />"; 
echo "Contact_number: "; 
echo "<br />"; 
echo $contact_number; 
echo "<br />"; 
echo "<br />"; 
echo "display_name: "; 
echo "<br />"; 
echo $display_name; 
echo "<br />"; 
echo "<br />"; 
echo "station: "; 
echo "<br />"; 
echo $station; 
echo "<br />"; 
echo "<br />"; 
echo "hobbies: "; 
echo "<br />"; 
echo $hobbies; 
echo "<br />"; 
echo "<br />"; 
echo "age: "; 
echo "<br />"; 
echo $age; 
echo "<br />"; 
echo "<br />"; 
*/ 
?> 

<?php 
////// SEND TO DATABASE 


///////////////////////////////////////////////////////// 

// Database Constants 
define("DB_SERVER", ""); 
define("DB_USER", ""); 
define("DB_PASS", ""); 
define("DB_NAME", ""); 

// 1. Create a database connection 
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS); 
if (!$connection) { 
    die("Database connection failed: " . mysql_error()); 
} 

// 2. Select a database to use 
$db_select = mysql_select_db(DB_NAME,$connection); 
if (!$db_select) { 
    die("Database selection failed: " . mysql_error()); 
} 
////////////////////////////////////////////////////////////// 
$query="INSERT INTO ptb_registrations (ID, 
first_name, 
last_name, 
email, 
display_name, 
date_of_birth, 
contact_number, 
station, 
hobbies, 
age, 
password 

) 
VALUES('NULL', 
'".$first_name."', 
'".$last_name."', 
'".$email."', 
'".$display_name."', 
'".$date_of_birth."', 
'".$contact_number."', 
'".$station."', 
'".$hobbies."', 
'".$age."', 
'".$password."' 
)"; 
mysql_query($query) or die ('Error updating database'); 
?> 
<?php 
function confirm_query($result_set) { 
       if (!$result_set) { 
        die("Database query failed: " . mysql_error()); 
       } 
     } 
function get_user_id() { 
    global $connection; 
    global $email; 
    $query = "SELECT * 
       FROM ptb_registrations 
       WHERE email = \"$email\" 
       "; 
     $user_id_set = mysql_query($query, $connection); 
     confirm_query($user_id_set); 
     return $user_id_set; 
     } 
?> 
<?php 
$user_id_set = get_user_id(); 
while ($user_id = mysql_fetch_array($user_id_set)) { 
    $cookie1 = "{$user_id["id"]}"; 
    setcookie("ptb_registrations", $cookie1, time()+3600); /* expire in 1 hour */ 

} 
?> 

<?php include ('send_email/reg_email.php'); ?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title>PlaytimeBoys Registration</title> 

    <link href="css/style.css" rel="stylesheet" type="text/css" /> 
    </head> 
    <body> 
    <div class="container"> 
     <h1>Thanks for Registering!</h1> 
     <h2>Please check your email to confirm your account has been set-up.</h2> 
     <p>&nbsp; </p> 
     <p>&nbsp;</p> 
     <p>&nbsp;</p> 
</div> 
</body> 
</html> 
<? ob_flush(); ?> 
+0

更改您的帳戶表包括一個帶有AUTO_INCREMENT的主鍵。 – ethrbunny

回答

1

你不需要處理這個PHP的。

最好在MySQL端做。

因此,在您的用戶表上,必須使用屬性AUTO_INCREMENT定義字段user_id。

這樣,插入到此表時不需要指定user_id參數。 相反,USER_ID將通過數據庫生成,爲整,由1

每次增加試試這個:

CREATE TABLE Users (
    user_id int(11) NOT NULL auto_increment, 
    first_name varchar(50), 
    last_name varchar(50) 
    PRIMARY KEY (user_id) 
) ENGINE=InnoDB 
1

你只需要更新您的數據庫,這樣做。 您可以通過運行該查詢修改USER_ID是自動增量

ALTER TABLE `tablename` MODIFY `user_id` INT(10) NOT NULL AUTO_INCREMENT 

,並當過您將進入你的數據庫,然後指定的user_id爲空值,如本

INSERT INTO 'tablename' (user_id, name, address) VALUES (null, name_value, address_value) 
相關問題