Hiya I was hoping for some help please,
I'm trying to insert records into my mysql database and then stay on the same page. I have 2 tables that relate (although the insert only goes into one of them):
rating table which has ->rating_id (AI), media_id (primary key from other table), ip and rating
media table which has --> media_id, media_title, other information.
So far my page selects all the records from the media table and outputs them. With each record I have a rating button that then it should check the database with a select to see if the ip address is not already in the rating table with the same media_id (to stop people rating more than once per item). If there is no records found then it should insert the record.
The trouble is its just not inserting anything, I'm not sure I understand why either. If you could help me fix it i'd be very grateful. Heres my relevant code:
if (isset($_POST['media_id'])) {
$media_id = mysql_real_escape_string($_POST['media_id']);
$ip = mysql_real_escape_string($_POST['ip']);
$rating = '1';
echo '$media_id';
echo '$ip';
echo '$rating';
// See if that product name is an identical match to another product in the system
$sql3 = mysql_query("SELECT * FROM rating WHERE media_id='$media_id' AND ip = '$ipaddress' LIMIT 1");
$productMatch = mysql_num_rows($sql3); // count the output amount
if ($productMatch > 0) {
echo 'Sorry you already rated this media';
exit();
}
// Add this product into the database now
$sql3 = mysql_query("INSERT INTO rating (media_id, ip, rating)
VALUES('$media_id','$ip','$rating')") or die (mysql_error());
$pid = mysql_insert_id();
header("location: books.php");
exit();
}
And the bits before the insert.
<?php
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$dynamicList = "";
$sql = mysql_query("SELECT * FROM media WHERE media_type = 'Book' ORDER BY media_title ASC");
$productCount = mysql_num_rows($sql); // count the output amount
//////////////////////////////////// Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////
$nr = $productCount; // Get total of Num rows from the database query
if (isset($_GET['pn'])) { // Get pn from URL vars if it is present
$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new)
//$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
} else { // If the pn URL variable is not present force it to be value of page number 1
$pn = 1;
}
//This is where we set how many database items to show on each page
$itemsPerPage = 5;
// Get the value of the last page in the pagination result set
$lastPage = ceil($nr / $itemsPerPage);
// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
if ($pn < 1) { // If it is less than 1
$pn = 1; // force if to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
$pn = $lastPage; // force it to be $lastpage's value
}
// This creates the numbers to click in between the next and back buttons
// This section is explained well in the video that accompanies this script
$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
} else if ($pn == $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> ';
} else if ($pn > 1 && $pn < $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
$sql2 = mysql_query("SELECT * FROM media WHERE media_type = 'Book' ORDER BY media_title ASC $limit");
//////////////////////////////// END Adam's Pagination Logic
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
// This shows the user what page they are on, and the total number of pages
$paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. ' ';
// If we are not on page 1 we can place the Back button
if ($pn != 1) {
$previous = $pn - 1;
$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
}
// Lay in the clickable numbers display here between the Back and Next links
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
// If we are not on the very last page we can place the Next button
if ($pn != $lastPage) {
$nextPage = $pn + 1;
$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
}
}
///////////////////////////////////// END Adam's Pagination Display Setup /
$ipaddress = $_SERVER["REMOTE_ADDR"];
if ($productCount > 0) {
while($row = mysql_fetch_array($sql2)){
$id = $row["media_id"];
$media_title = $row["media_title"];
$genre = $row["genre"];
$media_image = $row["media_image"];
$media_date = $row["media_date"];
$media_producer = $row["media_producer"];
$media_description = $row["media_description"];
$media_link = $row["media_link"];
$get_copy = $row["get_copy"];
$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="83%" valign="top" colspan="2"><h2 class="PostTitle">' . $media_title . '</h2>
<hr><br/>
</td></tr>
<tr><td colspan="2" valign="top"><img style="border:#666 1px solid;" src="media/' . $media_image . '.jpg" alt="' . $media_title . '" border="1" class="alignLeft"/></a>
<span class="MediaText"> <strong>Released:</strong> '. $media_date .' <br/>
<strong>Genre:</strong> '. $genre .' <br/>
<strong>Author:</strong> '. $media_producer .' <br/>
<strong>Description</strong><br/>'.$media_description.'</span>
</td></tr><tr><td colspan="2">
</td></tr><tr><td width="13%">
<form action="books.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">
<input type="hidden" name="pid" id="pid" value='.$id.' />
<input type="hidden" name="ip" id="ip" value='.$ipaddress.' />
<input type="hidden" name="rating" id="rating" value="1"/>
<span class="MediaText"><strong>Rate this Book</strong></span></td><td width="87%" align="left" valign="top">
<input type="submit" value="" name="button" id="button" class="button" /></form>
</td></tr><tr><td>
<span class="MediaText"><strong>Get Your Copy:</strong></span></td><td> '.$get_copy.' </td>
</tr><tr><td colspan="2"><br/><br/></td></tr>
</table>';
}} else {
$dynamicList = "We have no products listed in our store yet";
}
mysql_close();
Thank you very much.