回声表格数据?

时间:2012-12-29 21:21:30

标签: php mysql

我正在尝试制作一份注册表格,基本上我有4页,第一页要求提供名字,姓氏和电子邮件,第二页要求出生日期和密码,第3页会询问一些统计/复选框项目第4页将提交表格。

目前我正在尝试将一个表单中的数据从一个页面回显到第2,3页,然后最终在第4页一起提交到mysql数据库中。

我收到更新数据库邮件时出错。

有人可以告诉我,如果我做得对,或者有更简单的方法吗?

感谢;

<form class="" method="post" action="register_p2.php">
<div class="row first_name">
<input type="text" id="first_name" name="first_name" placeholder="First Name" />
</div>

<div class="row last_name">
<input type="text" id="last_name" name="last_name" placeholder="Last Name" />
</div>

<div class="row email">
<input type="email" id="email" name="email" placeholder="Email"  />
</div>

<input type="submit" value="Next >"  />

</form>

第2部分/第2页

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

<?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 registrations (Id,
first_name,
last_name,
email

 )
VALUES('NULL',
'".$first_name."',
'".$last_name."',
'".$email."'
)";
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 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("registrations", $cookie1, time()+3600);  /* expire in 1 hour */

}
?>


    <? ob_flush(); ?>

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <title>Register</title>

            <!-- The stylesheet -->
            <link rel="stylesheet" href="assets/css/styles.css" />

            <!--[if lt IE 9]>
              <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
            <![endif]-->
        </head>

        <body>
            <div id="main">

            <h2>Step 2: Security Details</h2>
            <br/>

                <form class="" method="post" action="register_p3.php">

                    <div class="row date_of_birth">
                        <input type="text" id="date_of_birth" name="date_of_birth" placeholder="D.O.B 10/02/1990" />
                    </div>

                    <div class="row password">
                        <input type="password" id="password" name="password" placeholder="Password" />
                    </div>

                    <div class="row password2">
                        <input type="password" id="password2" name="password2" placeholder="Password (Confirm)"  />
                    </div>

                    <input type="submit" value="Next >"  />

                </form>
            </div>
            <!-- JavaScript includes - jQuery, the complexify plugin and our own script.js -->
            <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
            <script src="assets/js/jquery.complexify.js"></script>
            <script src="assets/js/script.js"></script>     
        </body>
    </html>

2 个答案:

答案 0 :(得分:1)

使用session

尝试此操作
    <? ob_start(); 
         session_start();?>
<?php
// GET ACCOUNT INFORMATION FROM FORM AND ASSIGN VARIABLES
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
?>
<?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 />";
*/
?>

<?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 registrations (Id,
first_name,
last_name,
email

 )
VALUES('NULL',
'".$first_name."',
'".$last_name."',
'".$email."'
)";
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 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("registrations", $cookie1, time()+3600);  /* expire in 1 hour */

}
?>


    <? ob_flush(); ?>

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <title>Register</title>

            <!-- The stylesheet -->
            <link rel="stylesheet" href="assets/css/styles.css" />

            <!--[if lt IE 9]>
              <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
            <![endif]-->
        </head>

        <body>
            <div id="main">

            <h2>Step 2: Security Details</h2>
            <br/>

                <form class="" method="post" action="register_p3.php">

                    <div class="row date_of_birth">
                        <input type="text" id="date_of_birth" name="date_of_birth" placeholder="D.O.B 10/02/1990" />
                    </div>

                    <div class="row password">
                        <input type="password" id="password" name="password" placeholder="Password" />
                    </div>

                    <div class="row password2">
                        <input type="password" id="password2" name="password2" placeholder="Password (Confirm)"  />
                    </div>

                    <input type="submit" value="Next >"  />

                </form>
            </div>
            <!-- JavaScript includes - jQuery, the complexify plugin and our own script.js -->
            <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
            <script src="assets/js/jquery.complexify.js"></script>
            <script src="assets/js/script.js"></script>     
        </body>
    </html>

答案 1 :(得分:0)

如果您希望在多个页面上提供信息,则应将其存储在会话中。将$ _POST值存储到变量中时,只需执行类似的操作,并确保放置session_start();请拨打页面顶部。

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

$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email'] = $email;
?>

然后,您可以通过调用$ _SESSION ['first_name']或您想要访问的任何变量在任何页面上访问它们。一定要包含session_start();在每个需要使用这些变量的页面的顶部。