Jump to content

New To Php - Please Help W/ V Simple Comment Box


miocene
 Share

Recommended Posts

Mainly as a training exercise I am attempting to write a script for a simple "shoutbox" style comment box using php and mysql. My current progress can be viewed here.currently it displays all the items in the sql table.I would like it to have it display only the 10 last comments by having the code count the number of rows (records) in the table and using:

$result = mysql_query("SELECT * FROM shouts WHERE id > ($n_records - 10)");

Where $n_records is the number of records.Would this work? If so how do I count the number of records and place it as a numerical variable (e.g. $n_records = 36).I tried

$n_rows = mysql_query("SELECT COUNT(*) FROM shouts");

but it doesn't work so I echoed the variable $n_rows and it returns Resource id #2. (Whatever that is)Here is my code so far for retrieving the entries from the database and displaying them:

		<?php$con = mysql_connect("localhost","*******","******");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("miocene_shoutbox", $con);$n_rows = mysql_query("SELECT COUNT(*) FROM shouts");$result = mysql_query("SELECT * FROM shouts WHERE id < 5");while($row = mysql_fetch_array($result))  {  echo "<h4>" . $row['name'] . "</h4>" . "<p>" . $row['comment'] . "</p>";  }echo $n_rows;mysql_close($con);?>

Link to comment
Share on other sites

Just use LIMIT - much simpler.

SELECT * FROM shouts ORDER BY id DESC LIMIT 10

Link to comment
Share on other sites

OK sorry for the double post but another problem...I've added an image with some letters that a poster has to type in the form to prevent spambots posting.I've got it to work when the poster types the correct code but when they don't get it right nothing happens. How do I get a message to come up warning them of the wrong code entered?Currently my form POSTs the entries to my insert.php file which inserts the comment and title into my SQL table and uses header() to go back to the comment page.Here is the code of insert.php:

<?phpheader( 'Location: http://topuptents.x10hosting.com/index.php');?><?php$code = $_POST['code'];if ($code=="jopft"){  $con = mysql_connect("localhost","****","*****");  if (!$con)	{	die('Could not connect: ' . mysql_error());	}  mysql_select_db("miocene_shoutbox", $con);  $sql="INSERT INTO shouts (name, comment)  VALUES  ('$_POST[name]','$_POST[comment]')";  if (!mysql_query($sql,$con))   {	die('Error: ' . mysql_error());	}  mysql_close($con);}else{  die();}?>

Link to comment
Share on other sites

You just have a die statement in the else block. Change that part if you want it to do something other than quitting when they get the code wrong.
Sorry it looks like one of my other topics has formed the same as this one. Nevermind....I did this:
<?php$code= $_POST['code'];if ($code=="jopft"){header( 'Location: http://topuptents.x10hosting.com/index.php' );}else{header( 'Location: http://topuptents.x10hosting.com/wrongcode.php' );}?><?php$code= $_POST['code'];echo $code;if ($code=="jopft"){if (!$_POST['name']){die();}else{//INSERTION INTO TABLE CODE$con = mysql_connect("localhost","miocene","camera44");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("miocene_shoutbox", $con);$thetime = date('l jS \of F Y \a\t h:i a');$sql="INSERT INTO shouts (name, comment, date)VALUES('" . mysql_real_escape_string($_POST['name']) . "','" . mysql_real_escape_string($_POST['comment']) . "','$thetime')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }mysql_close($con);}}elsedie();?>

Is this a good way of doing it? Probably not the best programming practise but it works! try it

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...