Jump to content

Last 10 testimonials submit onto page


Genesis730

Recommended Posts

So I'm wanting to make a page that a textbox that submits to a database then pastes the last 10 submissions onto the page... Here's an outline of what i have... how it looks isn't important, the code is.

I have this form that collects the date, first name, last name, and testimonial and stores it in a database with an incrementing ID row...

What I'm not sure how to do is pull the last 10 posts and echo them onto the site.

Any help is appreciated...

 

---

 

<?php

// Connect to fake database :)

  mysql_connect("localhost","username","password") or die(mysql_error());

  mysql_select_db("testimonials") or die(mysql_error());

 

?>

 

<html>

 

<body>

 

<h1>TESTIMONIALS</h1>

<hr width="80%"><br /><br />

 

<?PHP

// Connection Error

if (!$conn) {

    echo "Sorry, but there seems to be a problem with the database.<br />Please try again later";

    exit;

}

 

if (!mysql_select_db("mydbname")) {

    echo "Sorry, but there seems to be a problem with the database.<br />Please try again later";

    exit;

}

 

?>

 

<form method="POST" action="testimonials.php">

 

<?PHP $date = date(' F jS Y'); ?>

 

<table cellspacing="0" cellpadding="0" border="0" align="center" width="50%">

  <tr>

    <td align="center">First Name<br />

    <input name="firstname" value="<?PHP echo $_POST['firstname']; ?>" />

    </td>

    <td align="center">Last Name<br />

    <input name="lastname" value="<?PHP echo $_POST['lastname']; ?>" />

    </td>

  </tr>

</table>

 

<textarea name="testimonials" rows="5" cols="40%"><?PHP echo $_POST['testimonials']; ?></textarea>

 

<p align="center"><input type="submit" name="submit" value="Submit My Testimonial" /></p>

</form>

 

<?PHP

if($_POST['submit']) {

 

//COLLECT DATA

$date = $_POST['date'];

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

$testimonial = $_POST['testimonial'];

 

//VALIDATION

 

$error = '';

 

if (!$firstname)

    $error = $error."<b>First Name</b><br />";

if (!$lastname)

    $error = $error."<b>Last Name</b><br />";

if (!$emailaddress)

    $error = $error."<b>Testimonial</b><br />";

 

if ($error!="")

echo "<span id='red'><font size='2'>Please fill out the following required fields:</font><br />$error</span>";

  else

  {

//SUBMIT DATA

mysql_query("INSERT INTO testimonials VALUES(`id`,'". $date ."','". $firstname ."','". $lastname ."','". $testimonial ."')") or die(mysql_error());

  }

 

}

?>

 

 

</body>

 

</html>

Link to comment
https://forums.phpfreaks.com/topic/187498-last-10-testimonials-submit-onto-page/
Share on other sites

For future reference, please use the


to surround code in :)

 

To display the testimonials:

 

$res = mysql_query("SELECT id, `date`, firstname, lastname, testimonial FROM testimonials ORDER BY date DESC LIMIT 10");

while ($row = mysql_fetch_assoc($res)) {
    echo "<b>{$row['firstname']} {$row['lastname']}</b><br />on {$row['date']}<br />{$row['testimonial']}<br /><br />";
}

 

Would be an example how to display them.

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.