Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.