php 为什么我的数据没有存储在session变量中?

vyswwuz2  于 2023-02-28  发布在  PHP
关注(0)|答案(1)|浏览(141)

我在阅读HTML输入和将数据存储在会话变量中时遇到了问题,最终目标是将这些数据插入MySQL表中,因此如果有更好的方法,我也会很感激:)

<?php
    session_start();
?>
<html>
    <head>
        <title> Blog space </title>
    <head>
    <body bgcolor = "EAEAEA">
        <h4> Register </h4>
        <form form method = "post" action = "InsertToDatabase.php">
            <fieldset>
                <legend style = "color:#0080c0;font-weight:bold">Register</legend>
                <table> 
                    <tr>
                        <td> Name: </td>
                        <td>
                            <input type = "text" name = "myName"/>
                        </td>
                    </tr>
                    <tr>                                
                        <td> Surname: </td>
                        <td> 
                            <input type = "text" name = "mySurname"/>
                        </td>
                    </tr>
                    <tr>
                        <td> Gender: </td>
                        <td>
                            <input type = "text" name = "myGender"/>
                        </td>
                    </tr>
                    <tr>
                        <td> Date of birth: </td>
                        <td> 
                            <input type = "date" name = "myDateOfBirth"/>
                        </td>
                    </tr>
                    <tr>
                        <td> Email: </td>
                        <td>
                            <input type = "email" name = "myEmail"/>
                        </td>
                    </tr>
                    <tr>    
                        <td> Contact: </td>
                        <td>
                            <input type = "text" name = "myContact">
                        </td>
                    </tr>
                </table>
                <input style="background-color:#46A5F9;
                            color:white; width:19mm"
                            type = "submit"
                            value = "Submit"/>                                  
            </fieldset>                 
        </form>   
    </body>
    <?php                       
        $_SESSION['name'] = $_POST['myName'];;
        $_SESSION['surname'] = $_POST['mySurname'];
        $_SESSION['gender'] = $_POST['myGender'];
        $_SESSION['dob'] = $_POST['myDateOfBirth'];
        $_SESSION['email'] = $_POST['myEmail'];
        $_SESSION['name'] = $_POST['myContact'];         
    ?> 
</html>

这是我的代码,请随时让我知道,如果我遗漏了任何重要的细节。

fd3cxomn

fd3cxomn1#

试试这个代码

<?php
session_start();

if (isset($_POST['submit'])) {
    $_SESSION['name'] = $_POST['myName'];
    $_SESSION['surname'] = $_POST['mySurname'];
    $_SESSION['gender'] = $_POST['myGender'];
    $_SESSION['dob'] = $_POST['myDateOfBirth'];
    $_SESSION['email'] = $_POST['myEmail'];
    $_SESSION['contact'] = $_POST['myContact'];
}
?>

<html>
<head>
    <title> Blog space </title>
</head>
<body bgcolor = "EAEAEA">
    <h4> Register </h4>
    <form method = "post" action = "InsertToDatabase.php">
        <fieldset>
            <legend style = "color:#0080c0;font-weight:bold">Register</legend>
            <table>
                <tr>
                    <td> Name: </td>
                    <td><input type = "text" name = "myName"/></td>
                </tr>
                <tr>
                    <td> Surname: </td>
                    <td><input type = "text" name = "mySurname"/></td>
                </tr>
                <tr>
                    <td> Gender: </td>
                    <td><input type = "text" name = "myGender"/></td>
                </tr>
                <tr>
                    <td> Date of birth: </td>
                    <td><input type = "date" name = "myDateOfBirth"/></td>
                </tr>
                <tr>
                    <td> Email: </td>
                    <td><input type = "email" name = "myEmail"/></td>
                </tr>
                <tr>
                    <td> Contact: </td>
                    <td><input type = "text" name = "myContact"></td>
                </tr>
            </table>
            <input style="background-color:#46A5F9;
                            color:white; width:19mm"
                   type = "submit"
                   name = "submit"
                   value = "Submit"/>
        </fieldset>
    </form>
</body>
</html>

相关问题