Jump to content

date post to db


sofia403

Recommended Posts

hello i have a question. i have a drop down form to select DD-MM-YEAR , but im unsure how to actually post it into one column DATE since values are given in three separate dropdown options?

 

here is an example of my html, and i would like a php code to be able to post that to mysql. i have it working for other dropdown menus, but not sure how would it work with a date. im very new to php and mysql and any help would be much appreciated. thank you.

 

<select name="Date" id="Date" class="regularfont"><option value="" selected="selected"></option>

<option value="1">01</option>

<option value="2">02</option>

 

</select> <select name="Date" id="Date" class="regularfont"><option value="" selected="selected"></option>

<option value="1">January</option>

<option value="2">February</option>

 

</select> <select name="Date" id="Date" class="regularfont"><option value="" selected="selected"></option>

<option value="2014">2014</option>

<option value="2013">2013</option>

Link to comment
Share on other sites

firstly change you select names to "Day" "Month" "Year" accordingly, then you could simply combine the 3 to form a legitimate date to insert into your db. like so

 

$date = $_POST['day'] . "-" . $_POST['Month'] . "-" . $_POST['Year'];

Link to comment
Share on other sites

thanks for quick response fugix,

 

however im having troubles still, not sure if i have everything entered correctly though.

 

i have a form which is being posted by php to mysql

 

here is my html:

 

<form name="DATE1" method="post">

<div><select name="day" id="Date1" class="regularfont"><option value="" selected="selected"></option>

<option value="1">01</option>

<option value="2">02</option>

</select> <select name="month" id="Date2" class="regularfont"><option value="" selected="selected"></option>

<option value="1">January</option>

<option value="2">February</option>

</select> <select name="year" id="Date3" class="regularfont"><option value="" selected="selected"></option>

<option value="2014">2014</option>

<option value="2013">2013</option>

 

 

on submit it passed on to php

 

<?php

$Date1 = $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year'];

?>

 

 

 

what am i doing wrong ?

Link to comment
Share on other sites

thanks for quick response fugix,

 

however im having troubles still, not sure if i have everything entered correctly though.

 

i have a form which is being posted by php to mysql

 

here is my html:

 

<form name="DATE1" method="post">

<div><select name="day" id="Date1" class="regularfont"><option value="" selected="selected"></option>

<option value="1">01</option>

<option value="2">02</option>

</select> <select name="month" id="Date2" class="regularfont"><option value="" selected="selected"></option>

<option value="1">January</option>

<option value="2">February</option>

</select> <select name="year" id="Date3" class="regularfont"><option value="" selected="selected"></option>

<option value="2014">2014</option>

<option value="2013">2013</option>

 

 

on submit it passed on to php

 

<?php

$Date1 = $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year'];

?>

 

 

 

what am i doing wrong ?

well, what is happening when the form is sent?

Link to comment
Share on other sites

i have some other forms in  a dropdown menu with different form names

 

those get passed on and entered into mysql by php, but the date one doesnt get updated, just 00-00-0000

 

should i post the whole html and php?

Link to comment
Share on other sites

i have some other forms in  a dropdown menu with different form names

 

those get passed on and entered into mysql by php, but the date one doesnt get updated, just 00-00-0000

 

should i post the whole html and php?

yes if you wouldn't mind, so i can see how you are going about this

Link to comment
Share on other sites

here is my HTML

 

<html>

<head>

<?php

$hostname = "localhost";

$db_user = "";

$db_password = "";

$database = "";

$db_table = "";

$db = mysql_connect($hostname, $db_user, $db_password);

mysql_select_db($database,$db);

?>

<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>

<script type="text/javascript" src="js/jquery.validate.min.js"></script>

<script type="text/javascript" src="js/additional-methods.min.js"></script>

</head>

<body>

 

 

<form id="signupForm" method="POST" action="processform.php">

<fieldset>

<legend>Signup Form</legend>

<label for="Country">*Country:</label>

<form name="Country" method="post">

<div><select name="Country">

<option value="">-----</option>

<option value="Canada">Canada</option>

<option value="UK">UK</option>

<option value="France">France</option></select></div></p>

 

<p><label for="Nationality">*Nationality:</label>

<form name="Nationality" method="post">

<div><select name="Nationality">

<option value="">-----</option>

<option value="Afghanistan">Afghanistan</option>

<option value="Albania">Albania</option>

<option value="Algeria">Algeria</option></select></div></p>

 

<p><label for="Province">*Province:</label>

<form name="Province" method="post">

<div><select name="Province">

<option value="">-----</option>

<option value="British Columbia">British Columbia</option>

<option value="Alberta">Alberta</option>

<option value="Saskatchewan">Saskatchewan</option></select></div></p>

 

<p><label for="DATE1">*Date:</label>

<form name="DATE1" method="post">

<div><select name="day" id="Date1" class="regularfont"><option value="" selected="selected"></option>

<option value="1">01</option>

<option value="2">02</option>

<option value="3">03</option></select>

 

<select name="month" id="Date2" class="regularfont"><option value="" selected="selected"></option>

<option value="1">January</option>

<option value="2">February</option>

<option value="3">March</option></select>

 

<select name="year" id="Date3" class="regularfont"><option value="" selected="selected"></option>

<option value="2014">2014</option>

<option value="2013">2013</option>

<option value="2012">2012</option></select></div></p></fieldset>

<p>

<div align="center"><input type="submit" name="submit" value="Submit your entry"></div>

</form>

</div></p><?php

}

?>

</body>

</html>

and my PHP processform.php

 

<?php

$hostname = "localhost";

$db_user = "";

$db_password = "";

$database = "";

$db_table = "";

$db = mysql_connect($hostname, $db_user, $db_password);

mysql_select_db($database,$db);

?>

 

<?php

if (isset($_REQUEST['submit'])) {

$sql = "INSERT INTO $db_table(Country,Nationality,Province,DATE1) values ('".mysql_real_escape_string(stripslashes($_REQUEST['Country']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Nationality']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Province']))."','".mysql_real_escape_string(stripslashes($_REQUEST['DATE1']))."')";

if($result = mysql_query($sql ,$db)) {

echo '<h1>Success!</h1>';

} else {

echo "ERROR: ".mysql_error();

}

} else

?>

 

 

<?php

$DATE1= $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year'];

?>

 

Link to comment
Share on other sites

Echo your query before you submit it. Plug it into a raw query line (like from the console or PHPMyAdmin) and see what errors are reported.

 

Chances are there is a problem with your query.

Link to comment
Share on other sites

Echo your query before you submit it. Plug it into a raw query line (like from the console or PHPMyAdmin) and see what errors are reported.

 

Chances are there is a problem with your query.

 

this is already implemented in her code

Link to comment
Share on other sites

yeah, use this

<?php
if (isset($_REQUEST['submit'])) {
$date1= $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year'];
$sql = "INSERT INTO $db_table(Country,Nationality,Province,DATE1) values ('".mysql_real_escape_string(stripslashes($_REQUEST['Country']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Nationality']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Province']))."','$date1')";
if($result = mysql_query($sql ,$db)) {
echo '<h1>Success!</h1>';
} else {
echo "ERROR: ".mysql_error();
}
} else 
?>

Link to comment
Share on other sites

While reading your PHP code I noticed alot of silly errors and mistakes, I've corrected everything I could see for you.

 

HTML Page

<html>
<head>
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/additional-methods.min.js"></script>
</head>
<body>

<form id="signupForm" method="POST" action="processform.php">
  <div>
    <fieldset>
      <legend>Signup Form</legend>
      <label for="Country">*Country:</label>
      <select name="Country">
      <option value="">-----</option>
      <option value="Canada">Canada</option>
      <option value="UK">UK</option>
      <option value="France">France</option></select></div></p>

      <p><label for="Nationality">*Nationality:</label>
      <div><select name="Nationality">
      <option value="">-----</option>
      <option value="Afghanistan">Afghanistan</option>
      <option value="Albania">Albania</option>
      <option value="Algeria">Algeria</option></select></div></p>

      <p><label for="Province">*Province:</label>
      <div><select name="Province">
      <option value="">-----</option>
      <option value="British Columbia">British Columbia</option>
      <option value="Alberta">Alberta</option>
      <option value="Saskatchewan">Saskatchewan</option></select></div></p>
      
      <p><label for="DATE1">*Date:</label>
      <div><select name="day" id="Date1" class="regularfont"><option value="" selected="selected"></option>
      <option value="1">01</option>
      <option value="2">02</option>
      <option value="3">03</option></select>

      <select name="month" id="Date2" class="regularfont"><option value="" selected="selected"></option>
      <option value="1">January</option>
      <option value="2">February</option>
      <option value="3">March</option></select>

      <select name="year" id="Date3" class="regularfont"><option value="" selected="selected"></option>
      <option value="2014">2014</option>
      <option value="2013">2013</option>
      <option value="2012">2012</option></select>
    </fieldset>
  </div>
  <p>
    <div align="center"><input type="submit" name="submit" value="Submit your entry"></div>
  </p>
</form>
<p>

</body>
</html>

 

PHP Processing Page

<?PHP
$hostname = "localhost";
$db_user = "";
$db_password = "";
$database = "";
$db_table = "";
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);


  if(isSet($_POST['submit'])) {
    $country     = mysql_real_escape_string($_POST['Country']);
    $nationality = mysql_real_escape_string($_POST['Nationality']);
    $province    = mysql_real_escape_string($_POST['Province']);
    $date        = $_POST['day'].'-'.$_POST['month'].'-'.$_POST['year'];

    $myQuery = "INSERT INTO {$db_table} (`Country`,`Nationality`,`Province`,`DATE1`) VALUES ('{$country}','{$nationality}','{$province}','{$date}')");

    if(mysql_query($myQuery)) {
      echo 'Record inserted.';
    } else {
      echo 'An error has occurred: '.mysql_error();
    }

  }
?>

 

Try using the above code and tell me how it goes.

 

Regards, PaulRyan.

Link to comment
Share on other sites

fugix it works! thanks for all your help! and thanks PaulRayn for correcting my mistakes, much appreciated! im trying to work on silly part :)

glad I could help, if you have any more problems we'll be here!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.