This however is not working !! when I check the table in phpmyadmin, the data is still the same when in fact it should have changed due to my two custom made functions !
<?php
/*
* The Function Below Connects to the Server and Selects the Necessary Database for the Jokemania Website
* @return (Boolean) and a Resource
*
*/
function db_connect() {
// Omitted Private Information. But connects to Db successfully.
}
function protect($input) {
db_connect();
mysql_real_escape_string($input);
return $input;
}
/*
* The Function Below Checks whether the given Joke ID and IP Address of the Visitor are present within the
* RATINGS Table inside our JOKES Database.
* @param Joke ID and IP Address of Visitor.
* @return Boolean
*
*/
function ipCheckUp($jID, $ipaddress) {
db_connect(); // This function (defined above) gives us access to the Database. (note this is a function call, within a function)
$query = "SELECT jokeid FROM ratings
WHERE
jokeid = %d AND ipaddress = '%s'"; // This is a MySQL Query that will grab the data (if it exists) from the RATINGS table.
$result = @mysql_query(sprintf($query, $jID, mysql_escape_string($ipaddress)));
return (bool)mysql_num_rows($result);
}
/*
* The Function Below Records the Visitor's IP Address along with the Joke ID of the Joke which He or She
* has decided to vote on (by clicking the Like button - Green Plus)
* RATINGS Table inside our JOKES Database.
* @param Joke ID and IP Address of Visitor.
* @return void
*
*/
function recVisitor($jID, $ipaddress) {
db_connect(); // This function (defined above) gives us access to the Database. (note this is a function call, within a function)
$query = "INSERT INTO ratings
SET
jokeid = %d, ipaddress = '%s'"; // This is a MySQL Query that will add the IP Address and Joke ID to the RATINGS table.
mysql_query(sprintf($query, $jID, mysql_escape_string($ipaddress)));
}
/*
* The Function Below Records the Visitor's IP Address along with the Joke ID of the Joke which He or She
* has decided to vote on (by clicking the Like button - Green Plus)
* RATINGS Table inside our JOKES Database.
* @param Joke ID and IP Address of Visitor.
* @return void
*
*/
function upRating($jID) {
db_connect(); // This function (defined above) gives us access to the Database. (note this is a function call, within a function)
$query = "UPDATE jokes SET rating = rating + 1 WHERE jokeid = %d "; // This is a MySQL Query that will Increase the Rating by 1 in the JOKES table.
mysql_query(sprintf($query, $jID));
}
$act = isset($_GET['act']) ? protect($_GET['act']): false;
if ($act == "runfunctions") {
recVisitor($jID, $ipaddress);
upRating($jID);
} ?>
Hopefully it’ll be easier to understand what’s going on with this
The $jID and $ipaddress are defined in the { index.php } file and that { functions.php } file is being included into the { index.php }.
Here is the bit from { index.php } that does the business.
<div id="jokesContainer">
<!-- The PHP Loop -->
<?php while ($row = mysql_fetch_array($result)) { ?>
<?php $jID = $row['jokeid']; // This is a variable to hold the Joke ID (jokeid).
$ipaddress = $_SERVER['REMOTE_ADDR'];?>
<div class="jokePost">
<div class="actBar">
<a href="#"><img src="images/report.png" alt="a little triangle with an exclamation mark" title="Report Joke" /></a>
<a href="index.php?act=runfunctions"><img class="right" src="images/like.png" alt="A little green plus sign"
title=" <?php if (ipCheckUp($jID, "$ipaddress")) {
echo "Already Liked";
} else {
echo "I Like";
} ?> " /></a>
</div>
<?php echo $row['jbody'] . '</div>';
} /* End Joke Post */
?><!-- End of PHP Loop -->
</div><!-- End of Jokes Container -->
I hope all is clear. I’ll be back in an hour or so … I have a Community Meeting to attend.
Okay, but here’s the thing - the act lines you’re talking about arnt inside a function definition.
That means that as soon as you include that file, those lines get executed.
Is $jID and $ipaddress defined at THAT point? I think you might be needing to change your link to…
Hmm I think I can understand the point your’re making.
You’re saying at THE POINT where I’m including the { functions.php } file, the { $jID & $ipaddress } are NOT YET defined. And that is why it isn’t working. Right ?
If I wanted to use a string variable called { $ipaddress } that stores the IP Address as a string and used that in my { act } code instead of the { $_SERVER[‘REMOTE_ADDR’] } … will I have to add another line to the { act } code and the anchor link code ?