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.

 

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.

Thanks, I will try to figure out what you said.  But here are the three files as plain text:

 

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

 

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

 

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

 

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.

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?

 

 

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.

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.

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.