Hi, everybody. I've been a member for a while, but I'm not a coder, so I haven't made any posts.
But I get frustrated not knowing how to do things. So I'm taking the plunge.
I'm working through a tutorial on PHP and MySQL (here: http://dev.mysql.com/tech-resources/articles/ddws/), and things have been going fine, right up until I try to write to the database using a form.
I've tried all the code and checked everything, and honestly, I'm starting to wonder whether there's a mistake in the code. I copied and pasted the code given on the site, and that doesn't seem to work either.
The code I'm using doesn't give an error; I click 'Submit', and the page reloads. When I check the database, the new information isn't in there.
Any pointers? Or any other tutorials you know of to work through? I'm basically just trying to learn how to build a MySQL database.
Thanks in advance. :)
<HTML>
<HEAD>
<TITLE>Gettin' Bizzy</TITLE>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</HEAD>
<BODY>
<H1>Test Page</H1>
<P>Server says! Today is
<?php
echo( date("l, F dS, Y.") );
?>
<?php
//First, connect to the database:
$dbconnect = mysql_connect("localhost", "myusername", "mypassword");
//Just in case there's a database connection problem, this will give an error message:
$dbconnect = @mysql_connect("localhost", "myusername", "mypassword");
if (!$dbconnect) {
echo( "<P>We're sorry. We cannot connect to the " .
"database server right now.</P>" );
exit();
}
//This chooses which database to use:
mysql_select_db("myusername_dbname", $dbconnect);
//And again, in case there are any connection problems, this gives an error message:
if (! @mysql_select_db("myusername_dbname") ) {
echo( "<P>We're sorry. That " .
"database is not available right now.</P>" );
exit();
}
?>
<H2>Words</H2>
<P>Here are all the words in the database:</P>
<BLOCKQUOTE>
<?php
//Request the actual words only
$result = mysql_query(
"SELECT Word FROM Dictionary");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}
// Display each word in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<P>" . $row["Word"] . "</P>");
}
?>
</BLOCKQUOTE>
<H2>Add a word to the dictionary</H2>
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
<P>Type your word here:<BR>
<TEXTAREA NAME="word" ROWS=2 COLS=40 WRAP></TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitWord" VALUE="SUBMIT">
</FORM>
<?php
if ("SUBMIT" == $submitWord) {
$sql = "INSERT INTO Dictionary SET " .
"Word='$word', " .
"AddDate=CURDATE()";
if (mysql_query($sql)) {
echo("<P>Your word has been added.</P>");
} else {
echo("<P>Error adding submitted word: " .
mysql_error() . "</P>");
}
}
?>
</BODY>
</HTML>