我有一个注册表单供用户注册我的网站。所有细节都会转到像名称等的mysql数据库。
当信息记录在数据库中时,我希望提交表单,以便创建顺序user_id,并将其与数据库中的名称和年龄等其他详细信息一起存储。
我需要什么代码?抱歉,我是php的新手。
我尝试使用像这样的cookie脚本,但它不会创建user_id。相反,数据库中的所有用户都有一个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 */
}
?>
继承我的完整代码:
<? 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> </p>
<p> </p>
<p> </p>
</div>
</body>
</html>
<? ob_flush(); ?>
答案 0 :(得分:1)
您不需要在PHP上处理此问题。
最好在MySQL端进行。
因此,在Users表中,必须使用属性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 :(得分:1)
您只需更新数据库即可。 您可以通过运行此查询将user_id修改为自动增量
ALTER TABLE `tablename` MODIFY `user_id` INT( 10 ) NOT NULL AUTO_INCREMENT
当您将条目插入数据库时,请将user_id指定为null,例如此
INSERT INTO 'tablename' (user_id, name, address) VALUES (null, name_value, address_value)