Hi Guys.
I am having serious problems with a script that won't insert the values posted from a form, to a table in my database.
For some reason, a row does infact get inserted into my database, but it is a row of blank values (which obviously, is no good to me).
Below, is my script and I have typed the name of the file above each extract, below:
Just to let you know, this script is an application I am building for a customer. It is just simply a review page where a satisfied/ or dissatisfied customers can post a review. And the review should show on the same page after posted.
Below, is the main page that displays the reviews and the form to post a review (this doc is probably the most relevant):
testimonials.php:
require_once "testimonialClass.php";
if( isset($_POST["send"]) ) {
processForm();
} else {
displayMainContent( array(), array(), new testimonial( array() ) );
}
function displayMainContent( $missingFields, $errorMessages, $testimonial_to_store ){
if( $missingFields ) {
echo "<div class='row'><div class='col-lg-8 col-lg-offset-2' style='background-color: red;'><p style='text-align: center; color: white; font-size: 30px; margin-top: 20px; margin-bottom: 20px;'>Missing fields! Please complete all fields correctly before submitting your review.</p></div></div>";
}
?>
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-8 col-sm-offset-2 col-xs-8 col-xs-offset-2" style="border: 1px solid black; border-radius: 15px; text-align: center; margin-top: 20px; background-color: #4169E1; margin-bottom: 40px;">
<div style="padding-top: 20px; padding-bottom: 20px;">
<p style="font-size: 33px; font-weight: bold; font-style: italic; font-family: century gothic; color: white;">Testimonials</p></div>
</div>
</div>
<div class="col-lg-9 col-lg-offset-3 col-md-9 col-md-offset-3" style="margin-bottom: 30px;">
<div class="col-lg-2 col-lg-offset-1 col-md-2 col-md-offset-1"><a href="contact.php"><img src="images/contact-icon.png" class="img-responsive threeicons" alt="Soda Blasting In Manchester"></a></div>
<div class="col-lg-2 col-md-2"><a href="services.php"><img src="images/service-icon.png" class="img-responsive threeicons" alt="Soda Blasting In Manchester"></a></div>
<div class="col-lg-2 col-md-2"><a href="images.php"><img src="images/gallery-icon.png" class="img-responsive threeicons" alt="Soda Blasting In Manchester"></a></div>
</div>
<div class="col-lg-10 col-lg-offset-1 col-md-10 col-md-offset-1 col-sm-12 col-xs-12 maincontentarea" style="border: 1px solid black; border-bottom: 0px solid white; text-align: center; margin-top: 60px; background-color: white;">
<div class="col-lg-10 col-lg-offset-1" style="margin-bottom: 90px;">
<div style="padding-top: 60px; padding-bottom: 30px;">
<h1 style="text-decoration: underline; font-weight: bold;">Recent Testimonials</h1></div>
<h2 style="text-decoration: underline; font-weight: bold; font-size: 25px; margin-bottom: 50px;">Submit A Review!</h2>
<?php
if( $missingFields ) {
echo "<div class='row' style='background-color: red;'><p style='font-size: 25px; color: white; margin-top: 15px; margin-bottom: 15px;'>Missing fields! Please complete all fields correctly.</p></div>";
} else {
?>
<p>Please read some of our recent testimonials, or leave us a review (if you are an exisiting customer) to help us further improve our service!</p>
<?php
}
list( $testimonials, $totalRows ) = testimonial::getTestimonials();
echo "<p style='margin-top: 50px;'><b>Displaying 1 - $totalRows Reviews:</b></p><br><br><br>";
$counter = 1;
foreach( $testimonials as $testimonial ) {
$counter++;
$color;
if( $counter % 2 == 0 ) {
$color = "#e6add8";
} else {
$color = "#ADD8E6";
}
echo "<div style='background-color: " . $color . " ; padding-top: 40px; margin-bottom: 7px;'>";
echo "<p style='font-size: 25px; font-style: italic; color: black;'>" . '"' . ucfirst($testimonial->getValueEncoded( "comment" )) . '"' . "</p><br>";
echo "<p style='font-size: 25px; font-style: italic;'>Star Rating: " . $testimonial->getValueEncoded( "rating" ) . " <img src='images/star.png' style='padding-bottom: 5px;'></p>";
echo "<p style='font-size: 15px; font-style: italic;'>" . ucfirst($testimonial->getValueEncoded( "firstname" )) . " " . ucfirst($testimonial->getValueEncoded( "lastname" )) . " - " . strtoupper($testimonial->getValueEncoded( "postcode" )) . "</p><br><br>";
echo "</div>";
}
?>
</div>
<div class="row" style="margin-bottom: 10px;">
<div class="col-lg-10 col-lg-offset-1" style="border: 1px solid black; background-color: #4169E1;">
<h3 style="margin-top: 30px; margin-bottom: 30px; font-weight: bold; font-family: century gothic; color: white; line-height: 30px;">Existing Customers - Please Leave Some Feedback:</h3>
</div>
</div>
<div class="row" style="margin-bottom: 20px;">
<div class="col-lg-10 col-lg-offset-1" style="border: 1px solid black; background-color: #f5f5f5;">
<form action="testimonials.php" method="post" style="margin-top: 40px; margin-bottom: 40px;">
<label for="rating">Star Rating ( Between 1 - 5 ):</label>
<select name="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select><br><br>
<label for="firstname">First Name:</label>
<input type="text" name="firstname" placeholder="Forename" value=""><br><br>
<label for="lastname">Last Name:</label>
<input type="text" name="lastname" placeholder="Surname" value=""><br><br>
<label for="postcode" style="vertical-align: middle;">Postcode<br> <span style="font-size: 12px;">( First 3 to 4 <br>Characters Only ):</span></label>
<input type="text" name="postcode" style="width: 120px;" placeholder="eg - SK16" value=""><br><br>
<label for="comment" style="vertical-align: top;">Feedback:</label>
<textarea name="comment" placeholder="Please leave us some feedback!"></textarea><br><br>
<input type="submit" name="send" value="Submit Feedback" style="margin-left: 35px;">
</form>
</div>
</div>
</div>
<?php
}
function processForm(){
$requiredFields = array( "rating", "firstname", "lastname", "postcode", "comment" );
$missingFields = array();
$errorMessages = array();
$testimonial_to_store = new testimonial( array(
"rating" => isset( $_POST["firstname"] ) ? preg_replace( "/[^ a-zA-Z]/", "", $_POST["firstname"] ) : "",
"rating" => isset( $_POST["lastname"] ) ? preg_replace( "/[^ a-zA-Z]/", "", $_POST["lastname"] ) : "",
"rating" => isset( $_POST["postcode"] ) ? preg_replace( "/[^ a-zA-Z0-9]/", "", $_POST["postcode"] ) : "",
"rating" => isset( $_POST["rating"] ) ? preg_replace( "/[^ 1-5]/", "", $_POST["rating"] ) : "",
"rating" => isset( $_POST["comment"] ) ? preg_replace( "/[^ \.\,\'\-a-zA-Z0-9]/", "", $_POST["comment"] ) : "" ));
if( $missingFields ) {
$errorMessages[] = "<p>Please complete all fields so we can submit your review!</p>";
}
if( $errorMessages ) {
displayMainContent( $missingFields, $errorMessages, $testimonial_to_store );
} else {
$testimonial_to_store->insert();
displayMainContent( array(), array(), new testimonial( array() ) );
}
}
........... I had to post the rest of my thread below, due the the 1000 character limit...