How to insert date in mysql using php

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
moinshaikh
Forum Commoner
Posts: 27
Joined: Tue Jan 18, 2011 3:40 am
Location: India

How to insert date in mysql using php

Post by moinshaikh »

I am developing add user form. The form collects many details along with birth date, now problem is that when i submit the form, everything works well, and goes to mysql db except birthdate, instead of birthdate entered,(using date picker - javascript), mysql shows 00-00-0000. Any solution please?

Thank you in advance...:)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: How to insert date in mysql using php

Post by Jade »

Where's the code you're using to insert the date into the database? That's what we'd need to see.
moinshaikh
Forum Commoner
Posts: 27
Joined: Tue Jan 18, 2011 3:40 am
Location: India

Re: How to insert date in mysql using php

Post by moinshaikh »

Edited by moderator to use html and php syntax tags, for readability. For future reference, please use the tags when posting code like this.
this is the html form code :

Code: Select all

<table width="300" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <form action="insert-customer.php" method="post" name="add_customer" onsubmit="add_customer_form_validation()">
            <td><table width="500" cellpadding="1">
                <tr>
                  <td colspan="3"><h3 class="form-text-titles">Add Customer</h3></td>
                </tr>
                <tr>
                  <td width="170" class="form-text-titles">ID Number </td>
                  <td width="7">:</td>
                  <td width="307"><input name="customer_id" type="text" size="20" id="cust_id" title="customer's id no." class="form-text-box"></td>
                </tr>
                <tr>
                  <td class="form-text-titles">First Name</td>
                  <td>:</td>
                  <td><input name="f_name" type="text" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td class="form-text-titles">Last Name</td>
                  <td>:</td>
                  <td><input name="l_name" type="text" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td class="form-text-titles">Street Address </td>
                  <td>:</td>
                  <td><input name="street" type="text" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td class="form-text-titles">City</td>
                  <td>:</td>
                  <td><input name="city" type="text" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td class="form-text-titles">Pincode</td>
                  <td>:</td>
                  <td><input name="pincode" type="text" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td class="form-text-titles">State</td>
                  <td>:</td>
                  <td><input name="state" type="text" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td class="form-text-titles">Nation</td>
                  <td>:</td>
                  <td><input name="nation" type="text" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td class="form-text-titles">Date of Birth </td>
                  <td>:</td>
                  <td><input name="dob" type="text" size="20" id="demo1" class="form-text-box"/>
                    <a href="javascript:NewCal('demo1','ddmmyyyy')"><img src="../../images/cal.gif" width="16" height="16" border="0" alt="Pick Birth Date" /></a></td>
                </tr>
                <tr>
                  <td class="form-text-titles">Gender</td>
                  <td>:</td>
                  <td class="form-text-titles"><input type="radio" name="gender" value="Male" checked="checked">Male
                      						   <input type="radio" name="gender" value="Female">Female
				  </td>
                </tr>
                <tr>
                  <td class="form-text-titles">Contact Number </td>
                  <td>:</td>
                  <td><input name="contact_no" type="text" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td class="form-text-titles">Email ID</td>
                  <td>:</td>
                  <td><input name="email_id" type="text" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td class="form-text-titles">ID Proof Name </td>
                  <td>:</td>
                  <td><select name="id_proof" class="form-text-box">
                      <option value="select_id_proof" selected="selected">Select ID Proof</option>
                      <option value="DrivingLicense">Driving License</option>
                      <option value="Passport">Passport</option>
                      <option value="PANCard">PAN Card</option>
                      <option value="ElectionCard">Election Card</option>
                      <option value="CollegeCard">College I Card</option>
                    </select>                  </td>
                </tr>
                <tr>
                  <td class="form-text-titles">ID Proof Reg. No. </td>
                  <td>:</td>
                  <td><input name="id_proof_reg_no" type="text" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td class="form-text-titles">ID Proof Expiry Date </td>
                  <td>:</td>
                  <td><input name="id_expiry_date" type="text" size="20" id="demo2" class="form-text-box"/>
                    <a href="javascript:NewCal('demo2','ddmmyyyy')"><img src="../../images/cal.gif" width="16" height="16" border="0" alt="Pick ID Proof Expiry Date" /></a></td>
                </tr>
                <tr>
                  <td class="form-text-titles">ID Proof Image </td>
                  <td>:</td>
                  <td><input name="id_proof_image" type="file" size="20" class="form-text-box"/></td>
                </tr>
                <tr>
                  <td colspan="3">&nbsp;</td>
                </tr>
                <tr>
                  <td colspan="3" align="right"><div align="center">
                    <input type="submit" name="submit" value="Register Now!" size="20" class="form-text-box"/>                    
                    <input type="reset" name="reset" value="Clear Form" size="20" class="form-text-box"/>
                  </div></td>
                </tr>
                
            </table></td>
          </form>
        </tr>
        <tr></tr>
      </table>

AND THIS IS THE PHP CODE THAT I USE TO INSERT DATA IN MYSQL :

Code: Select all

<?php
$con = mysql_connect("localhost","manager","manager");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("cybermanager", $con);

$sql="INSERT INTO customer (customer_id, f_name, l_name, street, city, pincode, state, nation, dob, gender, contact_no, email_id, id_proof_reg_no, id_expiry_date, id_proof_image)
VALUES ('$_POST[customer_id]','$_POST[f_name]','$_POST[l_name]','$_POST[street]','$_POST[city]','$_POST[pincode]','$_POST[state]','$_POST[nation]','$_POST[dob]','$_POST[gender]','$_POST[contact_no]','$_POST[email_id]','$_POST[id_proof_reg_no]','$_POST[id_expiry_date]','$_POST[id_proof_image]')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Customer Details added Successfully!";

mysql_close($con)
?>
moinshaikh
Forum Commoner
Posts: 27
Joined: Tue Jan 18, 2011 3:40 am
Location: India

Re: How to insert date in mysql using php

Post by moinshaikh »

remember that date's data type in mysql is date for one date field and datetime for second date field but both don't work.
mysql db screen shot
mysql db screen shot
mydql-db.jpg (184.35 KiB) Viewed 11772 times
Last edited by moinshaikh on Thu Feb 03, 2011 11:43 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to insert date in mysql using php

Post by califdon »

In MySQL, date and datetime values are input as a string, with the format 'YYYY-MM-DD', including the hyphens. You can use the PHP function strtotime (see http://php.net/manual/en/function.strtotime.php) and you may also find it useful to read this brief tutorial: http://www.tizag.com/mysqlTutorial/mysql-date.php.
Post Reply