0

I am new to php just to warn you. Anyway I've looked this up a bunch but seeing as I don't understand too much.. I can't see how to do this.. So I have a table with a 'date' column in it, and I am testing out using a form to insert values into the table.. I wanted to have it so I could split the date up in the form, (eg month: day: year) and have it combine them into one value to insert into the 'date' column.. any ideas how to do this?? I'd tried implode() but I'm not sure exactly how the values are sent from the form so.. With the codes:

The actual form

    <form action="submit.php" method="post" style="text-align: center; width: 550px; margin: auto;">
        <!--Id: <input type="text" name="id"> -->
        <div style="float: left;">Author: <input type="text" name="author"></div>
        <div style="float: right;">Month: <input type="text" name="month" size="3">
        Day: <input type="text" name="day" size="3">
        Year: <input type="text" name="year" size="5"><br></div>
        <textarea name="post" style="width: 550px; height: 100px;"></textarea><br>
        <input type="submit">
    </form>

</body>

the Submit.php

<?php

mysql_connect('localhost','root');
mysql_select_db('apple');

$date = implode("-", "(month, day, year)");

$sql = "INSERT INTO blog (id, author, $date, post) VALUES ('$_POST[id]', '$_POST[author]', '$_POST[date]', '$_POST[post]')";

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
?>

1 Answers1

2

implode() takes an array as its second parameter:

$date = implode("-", array($_POST['year'], $_POST['month'], $_POST['day']));

You can also use sprintf():

$date = sprintf("%04d-%02d-%02d", $_POST['year'], $_POST['month'], $_POST['day']);

There's also two problems with your query. Try this:

$sql = "INSERT INTO blog (id, author, `date`, post) 
        VALUES ('$_POST[id]', '$_POST[author]', '$date', '$_POST[post]')";

A few other points:

Zoe
  • 27,060
  • 21
  • 118
  • 148
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • I'm not sure how to reply to you directly so I hope I dont get in trouble for this but thank you for that c: I couldn't figure out how to get them to be listed like that.. idk why I didn't think of an array. Now though, it gives me an error: "Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-month-day, post) VALUES ('', 'author', '', 'maybe this will work??')' at line 1" edit: this does reply to you directly ignore that part :p – user1874212 Jan 26 '13 at 03:43
  • Thanks for all the tips btw :p I'm just testing everything out though idk, so it works now but it enters all dates as 0000-00-00 and I changed it from date to posted – user1874212 Jan 26 '13 at 03:54
  • Check the value of `$date` to make sure it contains the values you are expecting. My updated answer *may* also help. – John Conde Jan 26 '13 at 03:55
  • Your last update with adding the &_POST fixed it c: thank you so much, how do you check the value of the variable though? – user1874212 Jan 26 '13 at 03:58
  • just print it to your screen. That's it. :) – John Conde Jan 26 '13 at 03:59