Can't get simple insert code to work

My code is supposed to present the user with a form and when they hit submit, send the entered data as well as the current users username to the database table.
Then, eventually I will pull the data from the database table and present it on the page.
(It is a simple bulletin board.)

I am using Joomla 3.2.2 and Sorcerer to add my php code.

I can’t seem to get the simplest lines of code to do anything at all. I have tried countless possibilities from all over the internet and from 3 “Learn PHP” books that I have.

From all of my research, inserting data into the database is one of the most basic things of all yet, I can not make it happen for some reason and it is very frustrating.

The PHP code I have is below. This appears to work from the website view…but nothing gets added to my database table.

[php]

<?php $message = $_POST['postArea']; if(isset($_POST['submit'])){ mysql_query("INSERT INTO 'bulletin_board' VALUES('$message')"); Print "Added to Database"; } ?>

[/php]

Here is the code with the HTML form:

<body>

<?php

$message = $_POST['postArea'];

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

mysql_query("INSERT INTO 'bulletin_board' VALUES('$message')");

Print "Added to Database";

}

?>

<!--FORM POST-->
<form name="formPost" method="POST">
<fieldset>
<input name="postArea" type="text" maxlength="200" />
</fieldset>
<fieldset class = "buttonPost">
<input type = "hidden" name = "action" value = "insert" />
<p><input type = "submit" name="submit" value="Post"></p>
</fieldset>
</form>

</body>

mysql_ calls are deprecated. DO NOT use them. Not now, Not ever. USE PDO.

This post will get you on your way:

http://www.phphelp.com/forum/the-occasional-tutorial/beginners-pdo-bumpstart-code-use-it-now!

I tried using the “anyPageYouWant.php” example from the link you sent me. But still all I get is a blank white page in return.

 <?php
      $sql  = "SELECT message FROM bulletin_board WHERE message =?";
      $stmt = $pdo->prepare($sql);
      $stmt->execute(array(
          $_POST['message']
      ));
     
     $result = $stmt->fetchAll();
     
     if (count($result))
         {
         foreach ($result as $row)
             {
             echo '<pre>';
             print_r($row);
             echo '</pre>';
             }
         }
     else
         {
         echo "No rows returned.";
         }
     }
 ?>

You are missing the WHERE clause

WHERE id =?

** Assumes your unique message id is named id

If your using the files as posted , it should give you an error message. What does it say?

What are the field names in your database?

If you want everything in the db returned change your query to:

$sql = “SELECT* FROM bulletin_board”;

And change this line:

$stmt->execute(array(
$_POST[‘message’]
));

To

$stmt->execute();

That wont be processing anything from your form though. It will just give you all the records.

I added the WHERE message =? but still a blank white page. I can’t even view the page that my form is on.

Post all your files as you have them now.

{source}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
#displayBox {
height: auto;
width: 610px;
border: medium solid #000;
padding-top: 2px;
padding-bottom: 2px;
padding-right: 5px;
padding-left: 5px;
margin-top: 5px;
margin-right: auto;
margin-bottom: 5px;
margin-left: auto;
background-color: #D8D7D4; 
}
.postHeader {
background-color: #f4f0e7;
}
textarea {
margin-right: auto;
margin-left: auto;
width: 600px;
}
form {
text-align: center;
}

.buttonPost {
  text-align: left;
  width: 590px;
  margin-right: auto;
  margin-left: auto;
}
</style>
</head>

<body>

 <?php

      $sql  = "SELECT message FROM bulletin_board WHERE message =?";

      $stmt = $pdo->prepare($sql);

      $stmt->execute(array(

          $_POST['message']

      ));


     $result = $stmt->fetchAll();

     

     if (count($result))

         {

         foreach ($result as $row)

             {

             echo '<pre>';

             print_r($row);

             echo '</pre>';

             }

         }

     else

         {

         echo "No rows returned.";

         }

     }

 ?>

<!--FORM POST-->
<form name="formPost" method="POST">
<fieldset>

<input name="postArea" type="text" maxlength="200" />

</fieldset>
<fieldset class = "buttonPost">
<input type = "hidden" name = "action" value = "insert" />
<p><input type = "submit" name="submit" value="Post"></p>
</fieldset>
</form>

</body>
</html>
{/source}

That is going to do absolutely nothing. You need to download ALL the files I posted. The all work together.

Sponsor our Newsletter | Privacy Policy | Terms of Service