Jump to content

Message Board Problem


fredundant

Recommended Posts

Os I've developed a Message board. A user writes a message and then it appears on the message board. However When the user enters a message it isn't inserted into the MySql.

 

Also when the messages are entered manually directly into the MySql table they dont show on the site.

Can anyone see the problem.

 

The code is in 3 php scripts.

 

messageboard.php

<?php
;
session_start();
//this checks to see if the $_SESSION variable has been not set 
//or if the $_SESSION variable has been not set to true
//and if one or the other is not set then the user gets
//sent to the login page
if (!isset($_SESSION['username'])) {
    header('Location: http://kaaleigh.byethost15.com/login.php');
}




?>




<HTML>
<head><title>Message Board - Logged In</title>
<link rel='stylesheet' href='layout.css'>
</head>
<body bgcolor="#fd8ecf">
<center><img src="headerpage.jpg"></center>

<div class="navbar">
<div class="button"><a href="index.html">Home</a></div>
<div class="button"><a href="news.html">News</a></div>
<div class="button"><a href="gallery.html">Gallery</a></div>
<div class="button"><a href="videos.html">Videos</a></div>
<div class="button"><a href="contact.html">Contact</a></div>
<div class="button"><a href="links.html">Links</a></div>
<div class="button"><a href="msg.html">Message  Kaaleigh</a></div>

</div>

<div class="frame">

<frameset cols="25%,75%" noresize="noresize">

<?php
session_start();

$username = $_SESSION['username'];
$password = $_SESSION['password'];

  if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
    
  echo " <b>Welcome ".$username." <br><br></b>";
  }

    else {
    echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
    }

?>


<?php
mysql_connect("****************", "**********", "*********");
mysql_select_db("**************");
?>






<form action="message.php" method="POST">
Your Name: <input type="text" name="author"><br>
Message:<br><textarea cols="60" rows="5" name="message"></textarea><br>
<input type="submit" value="Post Message">
</form>



<hr>


<?php
// I am selecting everything from the messages section in the database and ordering them newest to oldest.
$sql = mysql_query("SELECT * FROM messages ORDER BY posted DESC");

// Now I am getting my results and making them an array
while($r = mysql_fetch_array($sql)) {

$posted = date("jS M Y h:i",$r[posted]);

// End of Array
}
?>


</body>


</html>

 

message.php

<?php
mysql_connect("*************", "*************", "**********");
mysql_select_db("**************");
$time = time();
mysql_query("INSERT INTO messages VALUES(NULL,'$_POST[message]','$_POST[author]','0','$time')");
echo "Message Posted.<br><a href='messageboard.php'>Return</a>";

 

msg.php

 

<?php
mysql_connect("********", "********", "*************");
mysql_select_db("**************");
echo "<a href='messageboard.php'>Go Back...</a>";
$sql = mysql_query("SELECT * FROM messages WHERE id = '$_GET[id]'");
// Now I am getting our results and making them an array
while($r = mysql_fetch_array($sql)) {
// Everything within the two curly brackets can read from the database using $r[]
// I need to convert the UNIX Timestamp entered into the database for when a thread...
// ... is posted into a readable date, using date().
$posted = date("jS M Y h:i",$r[posted]);
// Now this shows the thread with a horizontal rule after it.
echo "$r[message]<h4>Posted by $r[author] on $posted</h4><hr>";

// End of Array
}

 

 

Any thoughts?

Link to comment
https://forums.phpfreaks.com/topic/225434-message-board-problem/
Share on other sites

Ok, I know I am quite a novice myself (so I would recommend you waiting for clarification from a more experienced poster).

But where are you specifying what table column to insert the data?

 

eg an example code from w3schools using the following syntax as seen in all caps on the first line:

$sql="INSERT INTO Persons (COLUMN 1 , COLUMN 2, COLUMN 3)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

 

Edit:

Also, assuming you specified the columns that you are entering data, you would be able to ommit values such as the "NULL" (as I assume you are doing that because your first column is an auto incrementing ID field?). Although this probably will not fox your initial error, there's always a chance that the "NULL" is causing problems maybe.

As far as I'm aware you don't need to specify columns.

It takes the first value and puts it into the first column. Second into the second column etc.

 

I may be wrong, but thats how I was taught.

 

Yeah, it's not required. Just seems much cleaner to specify. I mean you are inserting a "NULL" value, when you could just specify the few columns where you actually want to insert data. Just me I guess.  :shrug:

 

Also to clarify, are you still unable to view a message if you go to msg.php?id=X (X being a post ID.)

 

 

ps: This post should probably be moved to PHP Coding Help. (I'm sure a moderator will get right on that)

As far as I'm aware you don't need to specify columns.

It takes the first value and puts it into the first column. Second into the second column etc.

 

I may be wrong, but thats how I was taught.

 

That only applies if you have a value for each and every field, otherwise you need to specify the fields explicitly.

The query is probably failing, but you have no logic in there to check for that, so . . .

 

$query = "INSERT INTO messages VALUES( NULL, '{$_POST['message']}', '{$_POST['author']}', '0' , '$time' )";
if( $result = mysql_query($query) ) {
if(mysql_affected_rows() > 0 ) {
	echo "Message Posted.<br><a href='messageboard.php'>Return</a>";
} else {
	echo 'There was an error posting your message. Please try again later.';
}
} else {
echo "There was a database error.";
// comment out next line for live site.
echo "<br>Query string: $query<br>Returned error: " . mysql_error() . '<br>';
}

Thanks

 

if i use msg.php?id=1

 

I get the message that was entered manually.

 

Pikachu2000 Thanks so much for your help. Yes its null because the id is AI.

 

The error i get with that code is

 

There was a database error.

Query string: INSERT INTO messages VALUES( NULL, 'hello this is a test', 'Fred', '0' , '1295810965' )

Returned error: Column count doesn't match value count at row 1

 

Im confused

 

 

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.