View Full Version : PHP/MySQL Interaction Help

24th December 2004, 11:53 PM
I'm in the process of making a very simple blog script. Basically, all this code should do, is grab the stuff in the two forms on the previous page, and enter it into the database. I've already created the database on my box. When I press submit on the form, it loads up submit.php, but just gives me a blank page and nothing is written to the database.

Help anyone? :)

<head><title>Post Submitted</title></head>
$content = $_POST['body'];
$subject = $_POST['subject'];
$time = date("h:iA");
$date = date("yyyy/mm/dd");

$user = "decryption";
$host = "localhost";
$password = "password";
$database = "Blog";
$connection = mysql_connect($host,$user,$password) or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select database");
$query = INSERT INTO Posts (time, date, content, subject) VALUES ('$time', '$date', '$content', '$subject');
$result = mysql_query($query) or die ("Couldn't execute query");

echo "Post submitted.";

25th December 2004, 04:32 AM
Maybe make a function? something to execute when u run the php code, Im pretty sure you will need that, as in your script it wount run anything thats why your getting a blank page all you have is variables nothing to do but get something to run them and it should work:)


25th December 2004, 05:00 AM
From the way i understand it when you pass variables via POST all you have to do in the recieving php script is have variables with the same name ie you text box is called 'name' you would have the variable $name...

also if you want that post submitted to show up you have to put it in html tags
ie echo "

Post submitted</p>"

the other thing ive found helps is echoing the database query on the page if it does appear to be working. Then you can see if your variables have been put into the query propery

ie echo &#036;query

And now that ive written all that.... you dont have any quotes around your query... should be

&#036;query = "INSERT INTO Posts (time, date, content, subject) VALUES (&#39;&#036;time&#39;, &#39;&#036;date&#39;, &#39;&#036;content&#39;, &#39;&#036;subject&#39;)";

EDIT: late night typo&#39;s :)

25th December 2004, 12:33 PM
Cheers bergerac&#33; Works the way it should :D
Now to learn how to get the data out of the database and into the browser :P

25th December 2004, 03:14 PM
hey decryption
if you need any help with php and sql give me a yell.

26th December 2004, 09:55 PM
Okay gents, another bug that&#39;s confusing my inexperienced mind.

The date is being written into the database as 0404-04-04. The time is written correctly though.

The date field in the database has the DATE data type assigned to it and the server has the correct date and time set to it.

Is there something I&#39;m overlooking?

26th December 2004, 10:18 PM
just use something like this,
&#036;insertSQL = "SET table SET date = NOW()"

That will give you the date in YYYY-MM-DD format

27th December 2004, 03:29 AM
Are you trying to insert a specific date or the date something was entered into the database?

If your trying to set the date something was entered set the date field in the database to the timestamp type and it automaticaly enters it for you when the record is created.

http://dev.mysql.com/doc/mysql/en/Date_and...e_overview.html (http://dev.mysql.com/doc/mysql/en/Date_and_time_type_overview.html)

21st January 2005, 08:07 PM
To get the YYYYMMDD formatting using PHP you can do a:

&#036;today = date ("Ymd");

I think if you read the docs on the date() function PHP Site (http://www.au.php.net) you will find that the characters within the date() function that you&#39;ve used each represent a part of the date.

E.g. a &#39;y&#39; represents a two digit year, so &#39;yyyy&#39; gives you &#39;04040404&#39;

Hope that makes sense.