Jump to content

Why won't this work?


z99tms

Recommended Posts

hi,

 

i've been following along with the MYSQL homepage's tutorial on getting a basic Joke database working via PHP. Everything went very well and I've found it to be excellent, but I can't get the database to take info from a form.

 

I've got a form there where I can enter in the new joke, a button which says submit, but nothing gets added?

I'd really appreciate some advice on this - I'm so close to getting something functional with this!

 

---------------------------------------------------------

<HTML>

<HEAD>

<TITLE> Our List of Jokes </TITLE>

<HEAD>

<BODY>

<?php

 

  // Connect to the database server

  $dbcnx = @mysql_connect("localhost",

          "root", "root");

  if (!$dbcnx) {

    echo( "<P>Unable to connect to the " .

          "database server at this time.</P>" );

    exit();

  }

 

  // Select the jokes database

  if (! @mysql_select_db("jokes") ) {

    echo( "<P>Unable to locate the joke " .

          "database at this time.</P>" );

    exit();

  }

 

?>

<P> Here are all the jokes in our database: </P>

<BLOCKQUOTE>

 

<?php

 

  // Request the text of all the jokes

  $result = mysql_query(

            "SELECT JokeText FROM Jokes");

 

  if (!$result) {

    echo("<P>Error performing query: " .

        mysql_error() . "</P>");

    exit();

  }

 

  // Display the text of each joke in a paragraph

  while ( $row = mysql_fetch_array($result) ) {

    echo("<P>" . $row["JokeText"] . "</P>");

  }

 

 

if ("SUBMIT" == $submitjoke) {

  $sql = "INSERT INTO Jokes SET " .

        "JokeText='$joketext', " .

        "JokeDate=CURDATE()";

  if (mysql_query($sql)) {

    echo("<P>Your joke has been added.</P>");

  } else {

    echo("<P>Error adding submitted joke: " .

        mysql_error() . "</P>");

  }

}

?>

 

<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>

<P>Type your joke here:<BR>

<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP></TEXTAREA><BR>

<INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">

</FORM>

 

</BLOCKQUOTE>

</BODY>

</HTML>

Link to comment
https://forums.phpfreaks.com/topic/38767-why-wont-this-work/
Share on other sites

try replacing this part:

 

if ("SUBMIT" == $submitjoke) {
  $sql = "INSERT INTO Jokes SET " .
         "JokeText='$joketext', " .
         "JokeDate=CURDATE()";
  if (mysql_query($sql)) {
    echo("<P>Your joke has been added.</P>");
  } else {
    echo("<P>Error adding submitted joke: " .
         mysql_error() . "</P>");
  }
}

 

with this:

 

if (isset($_POST['submit'])) {
  $sql = "INSERT INTO Jokes (`JokeText`,`JokeDate`) VALUES ('".$_POST['joketext']."', now())";
  if (mysql_query($sql)) {
    echo "<P>Your joke has been added.</P>";
  } else {
    echo "<P>Error adding submitted joke:".mysql_error() . "</P>";
  }
}

Link to comment
https://forums.phpfreaks.com/topic/38767-why-wont-this-work/#findComment-186253
Share on other sites

Change this...

 

 

if ("SUBMIT" == $submitjoke) {
  $sql = "INSERT INTO Jokes SET " .
         "JokeText='$joketext', " .
         "JokeDate=CURDATE()";
  if (mysql_query($sql)) {
    echo("<P>Your joke has been added.</P>");
  } else {
    echo("<P>Error adding submitted joke: " .
         mysql_error() . "</P>");
  }
}
?>

<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
<P>Type your joke here:<BR>
<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP></TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">
</FORM>

</BLOCKQUOTE>
</BODY>
</HTML>

 

 

To something like...

if ( ! empty ( $_POST['send'] ) && ! empty ( $_POST['joketext'] ) )
{
$text_test = trim ( $_POST['joketext'] );

if ( ! empty ( $text_test ) )
{
	mysql_query ( "INSERT INTO Jokes VALUES ( '" . mysql_real_escape_string ( $text_test ) . "', CURDATE() );" );

	if ( mysql_affected_rows () == 1 )
	{
		echo "<p>Your joke has been added.</p>";
	}
	else
	{
		echo "<p>Error adding submitted joke: " . mysql_error () . "</p>";
	}
}
}
?>
<FORM ACTION="<?php echo $_SERVER['PHP_SELF'];?>" METHOD=POST>
<input type='hidden' name='send' value='1'>
<P>Type your joke here:<BR>
<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP></TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">
</FORM>

</BLOCKQUOTE>
</BODY>
</HTML>

 

But the query format depends on your database table format. The problem you are having is a registered global issue. You must validate your form inputs using one of the newer SUPER GLOBALS ($_POST, $_GET, $_REQUEST)

 

 

Link to comment
https://forums.phpfreaks.com/topic/38767-why-wont-this-work/#findComment-186272
Share on other sites

try this then. to see if your actually getting values.

 

<HTML>
<HEAD>
<TITLE> Our List of Jokes </TITLE>
<HEAD>
<BODY>
<?php

  // Connect to the database server
  $dbcnx = @mysql_connect("localhost",
           "root", "root");
  if (!$dbcnx) {
    echo( "<P>Unable to connect to the " .
          "database server at this time.</P>" );
    exit();
  }

  // Select the jokes database
  if (! @mysql_select_db("jokes") ) {
    echo( "<P>Unable to locate the joke " .
          "database at this time.</P>" );
    exit();
  }

?>
<P> Here are all the jokes in our database: </P>
<BLOCKQUOTE>

<?php

  // Request the text of all the jokes
  $result = mysql_query("SELECT * FROM Jokes");
         
  if (!$result) {
echo "<P>Error performing query:".mysql_error() . "</P>";
    exit();
  }

  // Display the text of each joke in a paragraph
  while ( $row = mysql_fetch_array($result) ) {
    echo "<P>" . $row['JokeText'] . "</P>";
  }


if(isset($_POST['submit'])){

if($_POST['joketext'] == ""){
echo "No data submitted!";
}else{
$sql = "INSERT INTO Jokes (`JokeText`,`JokeDate`) VALUES ('{$_POST['joketext']}', now())";
if(mysql_query($sql)) {
echo "<P>Your joke has been added.</P>";
}else{
echo "<P>Error adding submitted joke:".mysql_error() . "</P>";
}
}
}
?>

<FORM ACTION="<?php echo $_SERVER['REQUEST_URI']; ?>" METHOD=POST>
<P>Type your joke here:<BR>
<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP></TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">
</FORM>

</BLOCKQUOTE>
</BODY>
</HTML>

Link to comment
https://forums.phpfreaks.com/topic/38767-why-wont-this-work/#findComment-187483
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.