0

我正在将一些表单数据插入到 sql 数据库中,但我还想插入一个不属于表单数据的帐户类型标签。

所以基本上表单数据可以很好地插入到数据库中,但我的数据库中还有一个名为“account_type”的列,我想将“member”这个词作为标准插入。

我可以这样做吗,因为我已经尝试将其添加到代码中,但它没有将“成员”插入到帐户类型中。

我尝试添加的代码:

<? $result = mysql_query("UPDATE ptb_registrations SET account_type=Member"); ?>

表单数据的其余插入代码:

<?php
    session_start();
    // other php code here


    $_SESSION['display_name'] = $_POST['display_name'];
    $_SESSION['password'] =  $_POST['password'];

    ?>

    <?php ob_start(); ?>
    <?php
    session_start();
    // GET ACCOUNT INFORMATION FROM FORM AND ASSIGN VARIABLES
    $first_name = $_SESSION['first_name'];
    $last_name = $_SESSION['last_name'];
    $email = $_SESSION['email'];
    $date_of_birth = $_SESSION['date_of_birth'];
    $contact_number = $_SESSION['contact_number'];
    $display_name = $_SESSION['display_name'];
    $password = $_SESSION['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 />";
    */
    ?>

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


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

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

    // 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,
    password

     )
    VALUES('NULL',
    '".$first_name."',
    '".$last_name."',
    '".$email."',
    '".$display_name."',
    '".$date_of_birth."',
    '".$contact_number."',
    '".$password."'
    )";
    mysql_query($query) or die ('Error updating database');
    ?>
    <?
    $result = mysql_query("UPDATE ptb_registrations SET account_type=Member");
    ?>
    <?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 ('includes/send_email/reg_email.php'); ?>

    <? ob_flush(); ?>
4

1 回答 1

0

为什么不在注册的同时插入呢?

$query="INSERT INTO ptb_registrations (ID,
    first_name,
    last_name,
    email,
    display_name,
    date_of_birth,
    contact_number,
    password,
    account_type
     )
    VALUES('NULL',
    '{$first_name}',
    '{$last_name}',
    '{$email}',
    '{$display_name}',
    '{$date_of_birth}',
    '{$contact_number}',
    '{$password}',
    'Member'
    )";

以下

<? $result = mysql_query("UPDATE ptb_registrations SET account_type='Member'"); ?>

where当您没有子句时,将导致 ALL account_type 等于 'Member' 。

要回答您的问题,很可能是因为您在更新命令中没有在 member 周围加上单引号,请参阅我的代码。

于 2013-01-28T18:09:00.943 回答