将值从select表单传递给mysql DB

时间:2015-09-18 16:12:28

标签: php html mysql select

我有一个mysql-db的简单问题。我想在我的db的一列中传递3个值,称为DateOfBirth

我制作了2个选择器,日期和月份,一个文本是年份(yer)。

我想将这些值组合到php变量并将其传递给VARCHAR的mysql列。 任何一个人都有解决方案吗?

 <?php
session_start();
if(isset($_SESSION['user'])!="")
{
    header("Location: home.php");
}
include_once 'dbconnect.php';

if(isset($_POST['btn-signup']))


{
    $dob = mysql_real_escape_string($_POST['day'.'month'.'yer']);


    if(mysql_query("INSERT INTO userprofile(gender,dateofbirth) VALUES('$dob')"))
    {
        ?>
        <script>alert('successfully registered ');</script>
        <?php
    }
    else
    {
        ?>
        <script>alert('error while registering you...');</script>
        <?php
    }
}
?>
<body>
<center>
    <div id="login-form">

        <form method="post">
            <table align="center" width="30%" border="0">

                <tr>
                    <td>
                        <select name="day">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                            <option value="6">6</option>
                            <option value="7">7</option>
                            <option value="8">8</option>
                            <option value="9">9</option>
                            <option value="10">10</option>
                            <option value="11">11</option>
                            <option value="12">12</option>
                            <option value="13">13</option>
                            <option value="14">15</option>
                            <option value="16">16</option>
                            <option value="17">17</option>
                            <option value="18">18</option>
                            <option value="19">19</option>
                            <option value="20">20</option>
                            <option value="21">21</option>
                            <option value="22">22</option>
                            <option value="23">23</option>
                            <option value="24">24</option>
                            <option value="25">25</option>
                            <option value="26">26</option>
                            <option value="27">27</option>
                            <option value="28">28</option>
                            <option value="29">29</option>
                            <option value="30">30</option>
                            <option value="31">31</option>
                        </select>


                        <select name="month">
                            <option value="1">January</option>
                            <option value="2">February</option>
                            <option value="3">March</option>
                            <option value="4">April</option>
                            <option value="5">May</option>
                            <option value="6">June</option>
                            <option value="7">July</option>
                            <option value="8">August</option>
                            <option value="9">September</option>
                            <option value="10">October</option>
                            <option value="11">November</option>
                            <option value="12">December</option>

                        </select>
                        <input class="yer" type="text" name="yer" placeholder="yer" required />
                    </td>
                </tr>

                    <td><button type="submit" name="btn-signup">Sign Me Up</button></td>
                </tr>
                <tr>
                    <td><a href="index.php">Sign In Here</a></td>
                </tr>
            </table>
        </form>
    </div>
</center>
</body>

1 个答案:

答案 0 :(得分:0)

更改行

$dob = mysql_real_escape_string($_POST['day'.'month'.'yer']);

$y = $_POST['yer'];
$m = $_POST['month'];
$d = $_POST['day'];
$dob = strtotime($y.'-'.$m.'-'.$a);

编辑:如果您要插入性别行,则需要提供性别值,否则它会抛出错误或继续将dob插入性别行然后抛出错误