Jump to content

I'm a newb at this...any help?


BNole

Recommended Posts

I posted this in another forum but I believe this seems to be the more appropriate area.Basically, I have a website and a big part of it is receiving/posting visitor comments. I've been manually inputting the comments onto the pages via HTML; however, i'd like to find a more automatic way that will also allow me to have the newest comment posted first. I'd like to still receive the comments via the form and into my email box. Then, i'd like to type them into some sort of database and have the website "pull" the comments onto the pages. Once again, i'm a newbie at this...and have no idea how to implement it. Thoughts?The website is: http://www.firecoachtiller.com/fanthefire.html

Link to comment
Share on other sites

Well, you use PHP,SQL and databased to do it. Check this part of the W3Schools tutorial and I think you should have it clear: http://w3schools.com/php/php_mysql_intro.asp
Yea, i've seen that...it's quite confusing. Haha. I found the section on selecting data from the database. Now, the question is, can I create this database manually? Making the form post to the database would be easier...but it's just another operation i'd struggle to accomplish. Seems like I might be better off keeping it as simple as possible.
Link to comment
Share on other sites

I don't think I'd be much help as I'm a noob as well, but I can tell you that oyu need a cmoputer with PHP and SQL installed on it, and you can install PHP MyAdmin to edit the database. But also you can use a form to automatically put the comment in the Database, then use SQL to display it. I can give you some code but I probably shouldn't try because I don't know it all. But if you'd like me to try and then wait for someone to confirm it I can.Oh and yes, the tutorial is confusing. Didn't help me at all.

Link to comment
Share on other sites

Here are the steps used to insert data to a database, with simplistic examples:1.<form action="add.php" method="post"><input type="text" name="message" /> *this is important, remember it</form>2.Inside the "add.php" file$msg = mysql_real_escape_string($_POST['message']); // mysql_real_escape_string() prevents messages from interfering with SQL code$con = mysql_connect("

[/i]","username","password");mysql_select_db("[i]database name[/i]",$con);mysql_query("INSERT INTO [i]database table name[/i]([i]field name[/i]) VALUES ('$msg')"); //This query adds the message to the [field name] of the table.
Link to comment
Share on other sites

Me? Well, after the removal of that line, it looks like...

<?php$con = mysql_connect("localhost","#####","#####");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("fanthefire", $con); mysql_query("INSERT INTO fanthefire (comments) VALUE('$_POST[message]');mysql_close($con);?>

Link to comment
Share on other sites

Ah, okay, I have seemed to fix that problem. Now, if someone doesn't mind, can you post the "code" it would take to post this same comment onto a piece of HTML? I'm pretty sure I can change it to be what i want once I see a simple example again.

Link to comment
Share on other sites

Connect to the database using mysql_connect() and mysql_select_db and then do this:$result = mysql_query("SELECT * FROM table ORDER BY something DESC"); //something would refer to the field you're ordering it by, like date or id. DESC orders it by the newest ones first.while($row = mysql_fetch_array($result)) {echo $row['message'];}

Link to comment
Share on other sites

Okay. One more question and I think i'll be able to leave you guys alone...How do you "capture" the data in one cell? For instance, I'm going to have 2 columns for each entry. If I want to get the data from the 50th row, first column, is there an easy way to do that? Like in visual basic you could simply do "Cell(row, column).value" and then I was able to ++ the row in order to grab the next row's data, etc. Is there something like that for php?

Link to comment
Share on other sites

You can use a MySQL SELECT statement, but you will have to know the name of the column

SELECT column_name FROM table LIMIT 50, 1

(Where the first 50 is the row number)

Link to comment
Share on other sites

I got it to work! Whew, that was some work. I know that it is probably a bad idea to echo HTML code...is there a proper way to do it? Below is the code. Like I said, it works...but i'm sure it can be better.

<?php$con = mysql_connect("localhost","*****","*****");    mysql_select_db("firecoac_fanthefire", $con);         for ($row=0; $row<=5; $row++)         {               $sql = "SELECT comments FROM fanthefire LIMIT $row, 1";               $result = mysql_query($sql,$con);               echo '<div class="comment">';                    echo mysql_result($result,0);               echo '</div>';               $sql = "SELECT signatures FROM fanthefire LIMIT $row, 1";               $result = mysql_query($sql, $con);                    echo '<div class="signature">';               echo mysql_result($result,0);               echo '</div><br /><br />';          }     mysql_close($con);?>

Link to comment
Share on other sites

That's not very efficient. If you're making 6 passes through the loop and you have 2 queries per loop that's 12 queries you're sending to the database. You could do the same thing with 1 query. If you want to get the first 6 rows with those two fields you can do this:

<?php$con = mysql_connect("localhost","*****","*****");mysql_select_db("firecoac_fanthefire", $con);$result = mysql_query("SELECT comments, signatures FROM fanthefire LIMIT 0, 6", $con);while ($row = mysql_fetch_assoc($result)){  echo '<div class="comment">';  echo $row['comments'];  echo '</div>';  echo '<div class="signature">';  echo $row['signatures'];  echo '</div><br /><br />';}mysql_close($con);?>

There's overhead involved with sending a query to the database. The fewer times you can send queries and accomplish the same thing, the better, in general at least. It's always a good idea to remove database queries from inside loop structures.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...