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. :)

<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>
4
Contributors
13
Replies
15
Views
7 Years
Discussion Span
Last Post by venkat0904
0

I cant see where are u retrieving the post variables before entering the word into the database.
ur insert statement

$sql = "INSERT INTO Dictionary SET " .
         "Word='$word', " .
         "AddDate=CURDATE()";

should be preceded by something like

$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. :)

<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

do you have a database created for it?
If not first create the database and check if you can connect to the mysql using

mysql_connect($server_name,$user,$password) or die(mysql_error());
0

I cant see where are u retrieving the post variables before entering the word into the database.
ur insert statement

$sql = "INSERT INTO Dictionary SET " .
         "Word='$word', " .
         "AddDate=CURDATE()";

should be preceded by something like

$word = $_POST['word']; //or whatever is the name of ur input field

hope that helps..

Thank you. I'll take a closer look.

do you have a database created for it?
If not first create the database and check if you can connect to the mysql using

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. :)

0

Okay, I've tried this:

<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>");
  }
}
?>

and for good measure, just to be sure, I tried this:

<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>");
  }
}
?>

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.

Edited by Sucesso: n/a

0

your code need lots of improvements like the insert query was wrong and the php too.Try this -

<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>");
  }
}
?>
0

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 -

<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>");
  }
}
?>
0

u really feel i need that!! plz correct me if i said somthin wrong..

Are you sure you don need this dose?

Edited by venkat0904: n/a

0

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

0

aahhh its fine... i was starting to get worried that after spending 2 months coding, some1 s suggesting me to read that absolute beginners stuff.. :P

Ahghh!
<sorry>
I don't meant you but the poster of thread!
It is great for beginners. I quoted a wrong person,
</Sorry>

Edited by venkat0904: n/a

0

Still not working for me. I don't know the code, so I'm not sure what to look for, but I made sure that all the variable names and everything were correct.

Here's the code that I'm using:

<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
$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>");
  }
}
?>

If this helps, when I use PuTTY to check the contents of the database, I see this:

mysql> select * from Dictionary;
+----+----------+------+----------------------------------------------+------------+
| ID | Word     | PoS  | Definition                                   | AddDate    |
+----+----------+------+----------------------------------------------+------------+
|  2 | abstract | adj  | NULL                                         | 0000-00-00 |
|  3 | abstract | adj  | NULL                                         | 0000-00-00 |
|  4 | abstract | adj  | not concrete; hard to understand; intangible | 2009-11-08 |
+----+----------+------+----------------------------------------------+------------+
3 rows in set (0.01 sec)

Are you sure you don need this dose?

Thanks! I'll look at that now.

0

why dont u use the code suggested above... ur sql query syntax itself is wrong.. how do you expect to get a database entry with that...
general syntax for insert statement is-
INSERT INTO [tablename] (col1,col2 ...) VALUES (val1, val2...);

Also ur table structure shows other fields also besides word and date... are u sure when u are inserting a new row, these values are allowed to be null?

Still not working for me. I don't know the code, so I'm not sure what to look for, but I made sure that all the variable names and everything were correct.

Here's the code that I'm using:

<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
$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>");
  }
}
?>

If this helps, when I use PuTTY to check the contents of the database, I see this:

mysql> select * from Dictionary;
+----+----------+------+----------------------------------------------+------------+
| ID | Word     | PoS  | Definition                                   | AddDate    |
+----+----------+------+----------------------------------------------+------------+
|  2 | abstract | adj  | NULL                                         | 0000-00-00 |
|  3 | abstract | adj  | NULL                                         | 0000-00-00 |
|  4 | abstract | adj  | not concrete; hard to understand; intangible | 2009-11-08 |
+----+----------+------+----------------------------------------------+------------+
3 rows in set (0.01 sec)

Thanks! I'll look at that now.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.