gpt4 book ai didi

php - 在提交注册表单时生成连续的 user_id

转载 作者:行者123 更新时间:2023-11-29 08:41:12 27 4
gpt4 key购买 nike

我有一个注册表单供用户注册我的网站。所有详细信息都会进入 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>&nbsp; </p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>
</body>
</html>
<? ob_flush(); ?>

最佳答案

您不需要在 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

关于php - 在提交注册表单时生成连续的 user_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13748002/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com