2

I have one simple problem with mysql-db. I want to pass 3 values in one column of my db that is called DateOfBirth.

I have made 2 selectors, day and month, and one text are for year (yer).

I want to combine these values to php variables and pass it to mysql column that is VARCHAR. Dose any one has solution for this?

 <?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>
5
  • What is gender doing in your insert query??? Commented Sep 18, 2015 at 16:16
  • 1
    you're telling the DB to expect two fields of data (gender,dateofbirth) and then providing only one bit of data (dob). Exactly what do you think should happen in this case? Commented Sep 18, 2015 at 16:18
  • 1
    you have 2 columns but 1 value; syntax error. and your else is NOT helping you. Commented Sep 18, 2015 at 16:23
  • Your column should be a date column, not varchar. Commented Sep 18, 2015 at 16:23
  • dbconnect.php <= Pandora's box as are $_POST and $_SESSION. and that isn't how $_POST works $_POST['day'.'month'.'yer'] Commented Sep 18, 2015 at 16:24

1 Answer 1

1

Change line

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

To

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

EDIT: If you're inserting into the gender row, you need to supply a value for gender, otherwise it's going to either throw an error or go ahead with the query inserting dob into the gender row and then throw an error

Sign up to request clarification or add additional context in comments.

2 Comments

why should they? you missed something
You don't have to escape strtotime and can be easily translated into whatever they want when retrieving from db without the extra step of converting it time since it's now already done before inserting into db.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.