| | |
Newbie question: PHP form not writing to MySQL database
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2008
Posts: 4
Reputation:
Solved Threads: 0
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.
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.

PHP Syntax (Toggle Plain Text)
<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>
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
should be preceded by something like
hope that helps..
ur insert statement
PHP Syntax (Toggle Plain Text)
$sql = "INSERT INTO Dictionary SET " . "Word='$word', " . "AddDate=CURDATE()";
PHP Syntax (Toggle Plain Text)
$word = $_POST['word']; //or whatever is the name of ur input field
hope that helps..
•
•
•
•
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.
PHP Syntax (Toggle Plain Text)
<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>
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
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
•
•
Join Date: Sep 2009
Posts: 521
Reputation:
Solved Threads: 61
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
If not first create the database and check if you can connect to the mysql using
PHP Syntax (Toggle Plain Text)
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
follow me on twitter
•
•
Join Date: Aug 2008
Posts: 4
Reputation:
Solved Threads: 0
0
#4 17 Days Ago
•
•
•
•
I cant see where are u retrieving the post variables before entering the word into the database.
ur insert statement
should be preceded by something likePHP Syntax (Toggle Plain Text)
$sql = "INSERT INTO Dictionary SET " . "Word='$word', " . "AddDate=CURDATE()";
PHP Syntax (Toggle Plain Text)
$word = $_POST['word']; //or whatever is the name of ur input field
hope that helps..
•
•
•
•
do you have a database created for it?
If not first create the database and check if you can connect to the mysql using
PHP Syntax (Toggle Plain Text)
mysql_connect($server_name,$user,$password) or die(mysql_error());
I'm going to try the suggestion above.
I appreciate the responses.
•
•
Join Date: Aug 2008
Posts: 4
Reputation:
Solved Threads: 0
0
#5 17 Days Ago
Okay, I've tried this: and for good measure, just to be sure, I tried this: 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.
php Syntax (Toggle Plain Text)
<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 $word = $_POST['submitWord']; 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>"); } } ?>
php Syntax (Toggle Plain Text)
<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 $word = $_POST['word']; 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>"); } } ?>
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.
•
•
Join Date: Sep 2009
Posts: 521
Reputation:
Solved Threads: 61
0
#6 17 Days Ago
your code need lots of improvements like the insert query was wrong and the php too.Try this -
PHP Syntax (Toggle Plain Text)
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD="post"> <P>Type your word here:<BR> <TEXTAREA NAME="word" ROWS="2" COLS="40" wrap="hard" ></TEXTAREA><BR> <INPUT TYPE="SUBMIT" NAME="submitWord" VALUE="SUBMIT"> </FORM> <?php if ( isset($_POST["submitWord"]) && $_POST["submitWord"]!='') { $word = $_POST['submitWord']; $sql = "INSERT INTO Dictionary (Word,AddDate) value( 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>"); } } ?>
"The discipline of writing something down is the first step towards making it happen."
follow me on twitter
follow me on twitter
0
#7 17 Days Ago
yeah gud point... bt network18 i believe instead of "value" it should be "values" in the query...
•
•
•
•
your code need lots of improvements like the insert query was wrong and the php too.Try this -
PHP Syntax (Toggle Plain Text)
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD="post"> <P>Type your word here:<BR> <TEXTAREA NAME="word" ROWS="2" COLS="40" wrap="hard" ></TEXTAREA><BR> <INPUT TYPE="SUBMIT" NAME="submitWord" VALUE="SUBMIT"> </FORM> <?php if ( isset($_POST["submitWord"]) && $_POST["submitWord"]!='') { $word = $_POST['submitWord']; $sql = "INSERT INTO Dictionary (Word,AddDate) value( 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>"); } } ?>
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
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
0
#8 17 Days Ago
•
•
•
•
yeah gud point... bt network18 i believe instead of "value" it should be "values" in the query...
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
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
0
#9 17 Days Ago
u really feel i need that!! plz correct me if i said somthin wrong..
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
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
![]() |
Similar Threads
- Inserting data to mysql database using php created form (MySQL)
- Help with php script to export mysql data and import data (PHP)
- How do I pass values with a link into mySQL database using PHP? (PHP)
- Creating User Manual in Vb6 and Populating TreeView from Mysql database VB6 (Visual Basic 4 / 5 / 6)
- Help! my script couldn't submit form data to mysql database (PHP)
- Parsing html form. (PHP)
- PHP MySQL Database Help (MySQL)
- Big question: lINE BREAKS IN PHP TEXT POSTED TO MYSQL DATABASE (PHP)
- PHP form problem. (PHP)
- Online voting (PHP)
Other Threads in the PHP Forum
- Previous Thread: Multiple Text areas not working.
- Next Thread: problem with image submit button
| Thread Tools | Search this Thread |
access advice ajax ajaxexample api array autocomplete automatically backup basic beginner button c# cakephp check checkbox class cms codes confirm curl database developers display downloader elearning email explodefunction files flash form forms glassfish google html http ibm image include indentedsubcategory insert java javascript jobs jquery keywords lamp libcurl limit link linux local login mail matching menu mergers methods multiple mysql mysqlquery newb news number oop oracle persist php post prime procedure programming python quality query radio remote script search security select server sms spam sql static stored sun syntax table tutorial upload validation video web webdevelopemnt website windows youtube zend







