Newbie question: PHP form not writing to MySQL database

Reply

Join Date: Aug 2008
Posts: 4
Reputation: Sucesso is an unknown quantity at this point 
Solved Threads: 0
Sucesso Sucesso is offline Offline
Newbie Poster

Newbie question: PHP form not writing to MySQL database

 
0
  #1
17 Days Ago
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.

  1. <HTML>
  2. <HEAD>
  3. <TITLE>Gettin' Bizzy</TITLE>
  4. <link rel="stylesheet" type="text/css" href="stylesheet.css">
  5. </HEAD>
  6. <BODY>
  7. <H1>Test Page</H1>
  8. <P>Server says! Today is
  9. <?php
  10. echo( date("l, F dS, Y.") );
  11. ?>
  12. <?php
  13.  
  14. //First, connect to the database:
  15.  
  16. $dbconnect = mysql_connect("localhost", "myusername", "mypassword");
  17.  
  18. //Just in case there's a database connection problem, this will give an error message:
  19.  
  20. $dbconnect = @mysql_connect("localhost", "myusername", "mypassword");
  21. if (!$dbconnect) {
  22. echo( "<P>We're sorry. We cannot connect to the " .
  23. "database server right now.</P>" );
  24. exit();
  25. }
  26.  
  27. //This chooses which database to use:
  28.  
  29. mysql_select_db("myusername_dbname", $dbconnect);
  30.  
  31. //And again, in case there are any connection problems, this gives an error message:
  32.  
  33. if (! @mysql_select_db("myusername_dbname") ) {
  34. echo( "<P>We're sorry. That " .
  35. "database is not available right now.</P>" );
  36. exit();
  37. }
  38. ?>
  39.  
  40. <H2>Words</H2>
  41. <P>Here are all the words in the database:</P>
  42. <BLOCKQUOTE>
  43. <?php
  44. //Request the actual words only
  45. $result = mysql_query(
  46. "SELECT Word FROM Dictionary");
  47. if (!$result) {
  48. echo("<P>Error performing query: " .
  49. mysql_error() . "</P>");
  50. exit();
  51. }
  52.  
  53. // Display each word in a paragraph
  54. while ( $row = mysql_fetch_array($result) ) {
  55. echo("<P>" . $row["Word"] . "</P>");
  56. }
  57. ?>
  58.  
  59. </BLOCKQUOTE>
  60. <H2>Add a word to the dictionary</H2>
  61.  
  62. <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
  63. <P>Type your word here:<BR>
  64. <TEXTAREA NAME="word" ROWS=2 COLS=40 WRAP></TEXTAREA><BR>
  65. <INPUT TYPE=SUBMIT NAME="submitWord" VALUE="SUBMIT">
  66. </FORM>
  67.  
  68. <?php
  69. if ("SUBMIT" == $submitWord) {
  70. $sql = "INSERT INTO Dictionary SET " .
  71. "Word='$word', " .
  72. "AddDate=CURDATE()";
  73. if (mysql_query($sql)) {
  74. echo("<P>Your word has been added.</P>");
  75. } else {
  76. echo("<P>Error adding submitted word: " .
  77. mysql_error() . "</P>");
  78. }
  79. }
  80. ?>
  81.  
  82. </BODY>
  83. </HTML>
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 141
Reputation: venkat0904 is an unknown quantity at this point 
Solved Threads: 14
venkat0904's Avatar
venkat0904 venkat0904 is offline Offline
Junior Poster
 
0
  #2
17 Days Ago
I cant see where are u retrieving the post variables before entering the word into the database.
ur insert statement
  1. $sql = "INSERT INTO Dictionary SET " .
  2. "Word='$word', " .
  3. "AddDate=CURDATE()";
should be preceded by something like
  1. $word = $_POST['word']; //or whatever is the name of ur input field

hope that helps..

Originally Posted by Sucesso View Post
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.

  1. <HTML>
  2. <HEAD>
  3. <TITLE>Gettin' Bizzy</TITLE>
  4. <link rel="stylesheet" type="text/css" href="stylesheet.css">
  5. </HEAD>
  6. <BODY>
  7. <H1>Test Page</H1>
  8. <P>Server says! Today is
  9. <?php
  10. echo( date("l, F dS, Y.") );
  11. ?>
  12. <?php
  13.  
  14. //First, connect to the database:
  15.  
  16. $dbconnect = mysql_connect("localhost", "myusername", "mypassword");
  17.  
  18. //Just in case there's a database connection problem, this will give an error message:
  19.  
  20. $dbconnect = @mysql_connect("localhost", "myusername", "mypassword");
  21. if (!$dbconnect) {
  22. echo( "<P>We're sorry. We cannot connect to the " .
  23. "database server right now.</P>" );
  24. exit();
  25. }
  26.  
  27. //This chooses which database to use:
  28.  
  29. mysql_select_db("myusername_dbname", $dbconnect);
  30.  
  31. //And again, in case there are any connection problems, this gives an error message:
  32.  
  33. if (! @mysql_select_db("myusername_dbname") ) {
  34. echo( "<P>We're sorry. That " .
  35. "database is not available right now.</P>" );
  36. exit();
  37. }
  38. ?>
  39.  
  40. <H2>Words</H2>
  41. <P>Here are all the words in the database:</P>
  42. <BLOCKQUOTE>
  43. <?php
  44. //Request the actual words only
  45. $result = mysql_query(
  46. "SELECT Word FROM Dictionary");
  47. if (!$result) {
  48. echo("<P>Error performing query: " .
  49. mysql_error() . "</P>");
  50. exit();
  51. }
  52.  
  53. // Display each word in a paragraph
  54. while ( $row = mysql_fetch_array($result) ) {
  55. echo("<P>" . $row["Word"] . "</P>");
  56. }
  57. ?>
  58.  
  59. </BLOCKQUOTE>
  60. <H2>Add a word to the dictionary</H2>
  61.  
  62. <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
  63. <P>Type your word here:<BR>
  64. <TEXTAREA NAME="word" ROWS=2 COLS=40 WRAP></TEXTAREA><BR>
  65. <INPUT TYPE=SUBMIT NAME="submitWord" VALUE="SUBMIT">
  66. </FORM>
  67.  
  68. <?php
  69. if ("SUBMIT" == $submitWord) {
  70. $sql = "INSERT INTO Dictionary SET " .
  71. "Word='$word', " .
  72. "AddDate=CURDATE()";
  73. if (mysql_query($sql)) {
  74. echo("<P>Your word has been added.</P>");
  75. } else {
  76. echo("<P>Error adding submitted word: " .
  77. mysql_error() . "</P>");
  78. }
  79. }
  80. ?>
  81.  
  82. </BODY>
  83. </HTML>
Gimme reputation points if u find my post helpful.
use [code] tags wherever applicable
dont start a new thread unless u cant find the topic already on forum.
mark a thread "solved" as soon as u get a solution
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 521
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 61
network18 network18 is offline Offline
Posting Pro
 
0
  #3
17 Days Ago
do you have a database created for it?
If not first create the database and check if you can connect to the mysql using
  1. mysql_connect($server_name,$user,$password) or die(mysql_error());
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 4
Reputation: Sucesso is an unknown quantity at this point 
Solved Threads: 0
Sucesso Sucesso is offline Offline
Newbie Poster
 
0
  #4
17 Days Ago
Originally Posted by venkat0904 View Post
I cant see where are u retrieving the post variables before entering the word into the database.
ur insert statement
  1. $sql = "INSERT INTO Dictionary SET " .
  2. "Word='$word', " .
  3. "AddDate=CURDATE()";
should be preceded by something like
  1. $word = $_POST['word']; //or whatever is the name of ur input field

hope that helps..
Thank you. I'll take a closer look.

Originally Posted by network18 View Post
do you have a database created for it?
If not first create the database and check if you can connect to the mysql using
  1. mysql_connect($server_name,$user,$password) or die(mysql_error());
Yes, I have the database, and I am able to connect to it and retrieve data. I have tested the code I pasted above, and it retrieves the data just fine. I just can't send data to the database.

I'm going to try the suggestion above.

I appreciate the responses.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 4
Reputation: Sucesso is an unknown quantity at this point 
Solved Threads: 0
Sucesso Sucesso is offline Offline
Newbie Poster
 
0
  #5
17 Days Ago
Okay, I've tried this:
  1. <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
  2. <P>Type your word here:<BR>
  3. <TEXTAREA NAME="word" ROWS=2 COLS=40 WRAP></TEXTAREA><BR>
  4. <INPUT TYPE=SUBMIT NAME="submitWord" VALUE="SUBMIT">
  5. </FORM>
  6.  
  7. <?php
  8. $word = $_POST['submitWord'];
  9. if ("SUBMIT" == $submitWord) {
  10. $sql = "INSERT INTO Dictionary SET " .
  11. "Word='$word', " .
  12. "AddDate=CURDATE()";
  13. if (mysql_query($sql)) {
  14. echo("<P>Your word has been added.</P>");
  15. } else {
  16. echo("<P>Error adding submitted word: " .
  17. mysql_error() . "</P>");
  18. }
  19. }
  20. ?>
and for good measure, just to be sure, I tried this:
  1. <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
  2. <P>Type your word here:<BR>
  3. <TEXTAREA NAME="word" ROWS=2 COLS=40 WRAP></TEXTAREA><BR>
  4. <INPUT TYPE=SUBMIT NAME="submitWord" VALUE="SUBMIT">
  5. </FORM>
  6.  
  7. <?php
  8. $word = $_POST['word'];
  9. if ("SUBMIT" == $submitWord) {
  10. $sql = "INSERT INTO Dictionary SET " .
  11. "Word='$word', " .
  12. "AddDate=CURDATE()";
  13. if (mysql_query($sql)) {
  14. echo("<P>Your word has been added.</P>");
  15. } else {
  16. echo("<P>Error adding submitted word: " .
  17. mysql_error() . "</P>");
  18. }
  19. }
  20. ?>
But neither worked--same result.

I appreciate the patience. I understand the concepts at work here, but I'm not completely certain of how the parts of the code are working together.
Last edited by Sucesso; 17 Days Ago at 3:30 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 521
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 61
network18 network18 is offline Offline
Posting Pro
 
0
  #6
17 Days Ago
your code need lots of improvements like the insert query was wrong and the php too.Try this -
  1. <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD="post">
  2. <P>Type your word here:<BR>
  3. <TEXTAREA NAME="word" ROWS="2" COLS="40" wrap="hard" ></TEXTAREA><BR>
  4. <INPUT TYPE="SUBMIT" NAME="submitWord" VALUE="SUBMIT">
  5. </FORM>
  6.  
  7. <?php
  8. if ( isset($_POST["submitWord"]) && $_POST["submitWord"]!='')
  9. {
  10. $word = $_POST['submitWord'];
  11.  
  12. $sql = "INSERT INTO Dictionary (Word,AddDate) value( Word='".$word."',AddDate='".CURDATE()."')";
  13. if (mysql_query($sql)) {
  14. echo("<P>Your word has been added.</P>");
  15. } else {
  16. echo("<P>Error adding submitted word: " .
  17. mysql_error() . "</P>");
  18. }
  19. }
  20. ?>
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 141
Reputation: venkat0904 is an unknown quantity at this point 
Solved Threads: 14
venkat0904's Avatar
venkat0904 venkat0904 is offline Offline
Junior Poster
 
0
  #7
17 Days Ago
yeah gud point... bt network18 i believe instead of "value" it should be "values" in the query...

Originally Posted by network18 View Post
your code need lots of improvements like the insert query was wrong and the php too.Try this -
  1. <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD="post">
  2. <P>Type your word here:<BR>
  3. <TEXTAREA NAME="word" ROWS="2" COLS="40" wrap="hard" ></TEXTAREA><BR>
  4. <INPUT TYPE="SUBMIT" NAME="submitWord" VALUE="SUBMIT">
  5. </FORM>
  6.  
  7. <?php
  8. if ( isset($_POST["submitWord"]) && $_POST["submitWord"]!='')
  9. {
  10. $word = $_POST['submitWord'];
  11.  
  12. $sql = "INSERT INTO Dictionary (Word,AddDate) value( Word='".$word."',AddDate='".CURDATE()."')";
  13. if (mysql_query($sql)) {
  14. echo("<P>Your word has been added.</P>");
  15. } else {
  16. echo("<P>Error adding submitted word: " .
  17. mysql_error() . "</P>");
  18. }
  19. }
  20. ?>
Gimme reputation points if u find my post helpful.
use [code] tags wherever applicable
dont start a new thread unless u cant find the topic already on forum.
mark a thread "solved" as soon as u get a solution
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,357
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso
 
0
  #8
17 Days Ago
Originally Posted by venkat0904 View Post
yeah gud point... bt network18 i believe instead of "value" it should be "values" in the query...
Are you sure you don need this dose?
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 141
Reputation: venkat0904 is an unknown quantity at this point 
Solved Threads: 14
venkat0904's Avatar
venkat0904 venkat0904 is offline Offline
Junior Poster
 
0
  #9
17 Days Ago
u really feel i need that!! plz correct me if i said somthin wrong..

Originally Posted by evstevemd View Post
Are you sure you don need this dose?
Last edited by venkat0904; 17 Days Ago at 5:40 am.
Gimme reputation points if u find my post helpful.
use [code] tags wherever applicable
dont start a new thread unless u cant find the topic already on forum.
mark a thread "solved" as soon as u get a solution
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,357
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso
 
0
  #10
17 Days Ago
Oh yes, that is the humour way of saying that you need that you need that tutorial. It is great by a popular writer of the books
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Reply

Tags
beginner, forms, mysql, php

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC