0

我有一个注册表格供用户注册我的网站。所有详细信息都转到 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(); ?>
4

2 回答 2

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)
于 2012-12-06T16:47:03.847 回答
1

你不需要在 PHP 上处理这个。

最好在 MySQL 端进行。

因此,在您的用户表上,字段 user_id 必须使用属性 AUTO_INCREMENT 定义。

这样,插入此表时无需指定 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
于 2012-12-06T16:45:20.010 回答