Jump to content

[SOLVED] Insert date into MySQL db using drop downs


outchy

Recommended Posts

I have a php form for inserting concert information into a MySQL database.  Right now, I am manually entering the date in YYYY-MM-DD format and it's working good.  But, I'd like to have 3 separate drop down boxes (month, day and year) and have their values be combined and submitted into the database as the variable 'dateofshow' (with the same YYYY-MM-DD format).  I can make the drop downs no problem, I just need help getting the backend to work.  I've tried all of the examples I could find online, but I just can't get any of them to work :/

 

I've zipped up my three .php files here:

 

http://www.warehamps.org/nik/mysqlhelp.zip

 

Any help would be MUCH appreciated.

 

Link to comment
Share on other sites

i've not looked at the phpfiles you uploaded because i'm in a club on my phone but something along the lines of:

CONCAT($_POST['year'], "-", $_POST['month'], "-", $_POST['day'])

Where you would usualy enter "$dateofshow" (or "$_POST['dateofshow']") should work if you're actualy pulling out in the YYYY-MM-DD format.

Link to comment
Share on other sites

Well, if you had 3 drop down boxes for the form to add a show called "year", "month" and "day". In insert.php you would put something like:

<?php
$con = mysql_connect("localhost","db","psw");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("db", $con);
$sql="INSERT INTO shows (dateofshow, headliner, place, opener)
VALUES
(CONCAT($_POST['year'], "-", $_POST['month'], "-", $_POST['day']),'$_POST[headliner]','$_POST[place]','$_POST[opener]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 show added";
mysql_close($con)
?>

which would enter the date as: YYYY-MM-DD.

Link to comment
Share on other sites

Hmm, that didn't quite work.  When I try submitting the form, it just goes to a blank page.  Is this line supposed to add the day, month and year values together to the 'dateofshow' variable that exists in the database?

 

(CONCAT($_POST['year'], "-", $_POST['month'], "-", $_POST['day']),'$_POST[headliner]','$_POST[place]','$_POST[opener]')";

 

Sorry, I'm a bit of a noob.

 

Here are what the files look like now on my test server:

 

http://www.warehamps.org/nik/index2.txt

 

http://www.warehamps.org/nik/insert2.txt

 

http://www.warehamps.org/nik/add2.txt

 

Any ideas what's going wrong?

 

 

Link to comment
Share on other sites

Please echo $sql.

 

Okay, how do I do that?  ???

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("nik", $con);
$sql="INSERT INTO test (dateofshow, headliner, place, opener)
VALUES
(CONCAT($_POST['year'], "-", $_POST['month'], "-", $_POST['day']),'$_POST[headliner]','$_POST[place]','$_POST[opener]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo $sql;
echo "1 record added";
mysql_close($con)
?>
<br /><br />
<a href="http://localhost:8888/test.php">Back</a>

Then submit the page as usual, when you get the blank screen look at the page source and post the sql that is shown in the page source, here.

Link to comment
Share on other sites

lol, i just realised something... give this a try

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("nik", $con);
$sql="INSERT INTO test (dateofshow, headliner, place, opener)
VALUES
(CONCAT($_POST['year'], '-', $_POST['month'], '-', $_POST['day']),'$_POST[headliner]','$_POST[place]','$_POST[opener]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo $sql;
echo "1 record added";
mysql_close($con)
?>
<br /><br />
<a href="http://localhost:8888/test.php">Back</a>

my double quotes were possibly closing the query prematurely.

 

If it runs fine, remove the "echo $sql;" line, if not - post the result from it.

Link to comment
Share on other sites

Hooker just PM'd me this and it totally worked!

 

(CONCAT('$_POST[year]','-','$_POST[month]','-','$_POST[day]'),'$_POST[headliner]','$_POST[place]','$_POST[opener]')";

 

To all of you really, thank you so much!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.