I've just knocked this up quickly so it may not work perfectly
There are two files...one of which is include.php which contains the connection information needed to access the database. This file (include.php) should be placed in the directory above the current file for security.
Include.php:
PHP Code:
<?php
function db_connect()
{
$username = "";
$password = "";
$database = "";
$handle = mysql_connect ("localhost", "$username", "$password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db('gkelly_siteopening', $handle) or die('Could not select database.');
return $handle;
}
?>
Product.php:
PHP Code:
<?php
//Include a file include.php which is above the current directory (so can't be accessed directly from a browser = more security)
require_once('../include.php');
if(isset($_POST['validation'])) {
//Put ID in URL into $id variable
$id = $_REQUEST['id'];
$name_rev=$_POST['name'];
$email_rev=$_POST['email'];
$rating_rev=$_POST['rating'];
$review_rev=$_POST['review'];
//Check if the form was filled out correctly...if not print an error and change $error variable to true
$error = false
if (empty($name_rev)){echo 'You forgot to enter your name'; $error = true;}
if (empty($email_rev)){echo 'You forgot to enter your email'; $error = true;}//remove this line if the email isn't necessary
if (empty($review_rev)){echo 'You forgot to write a review'; $error = true;}
//Only continue if the form was filled out correctly
if ($error = false){
//Do some basic handling of submitted stuff:
//Make sure the rating is a number
$rating_rev=doubleval($rating_rev);
//Add slashes to the text to prevent problems occuring [remember to use stripslashes(...) when printing the review]
$email_rev=addslashes(email_rev);
$review_rev=addslashes(review_rev);
$name_rev=addslashes(name_rev);
//Open database connection by calling function in include.php
$dbh=db_connect();
$query_review = "INSERT INTO restreview VALUES ('$id','$name_rev','$email_rev','$rating_rev','$review_rev')";
//Run the query and if it fails print an error
if (!$result = $mysql_query($query_review)){
$echo 'An error has occured. Please try again later.';
} else {
echo 'Your review was successfully submitted';
}
}//end if ($error = false){...
mysql_close();
}
?>
<h1>Your Details:</h1>
<form action="product.php?id=<? echo $id; ?>" method="post">
<span class="det">Name:</span> <input style="margin-left: 5px;" type="text" name="name"><br/>
<span class="det">Email:</span> <input style="margin-left: 7px;" type="text" name="email"> <span class="det">(will not be displayed)</span><br/>
<span class="det">Rating:</span> <select name="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected="selected">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br/>
<h1>Review:</h1>
<textarea rows="5" cols="50" name="review">
Enter your review here
</textarea>
<input type="hidden" name="validation" value="1"/>
<p style="margin-left: 50px;" >
<input type="submit" value="Submit" >
<input type="reset" value="Reset">
</p>
</form>
Hope this helps
Jamie