craigerjs Posted July 25, 2010 Share Posted July 25, 2010 Hi, I am trying to build a rating system for images. I have a loop that displays the images and I want to add a voting option for each image. Right now when you vote on one image, the vote gets recorded for each image in the loop. How can I make the vote script unique for each image? Here is the script that displays the images: <?php // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to MySQL server.'); // Retrieve the id data from MySQL $query = "SELECT * FROM user_images ORDER BY id DESC, date ASC"; $data = mysqli_query($dbc, $query); // Loop through the array of id data, formatting it as HTML echo '<table border="0" cellspacing = "20">'; while ($row = mysqli_fetch_array($data)) { // Display the images from newest to oldest echo '<tr><td valign="top"width = 260px height = 600px>'; echo '<strong>User:</strong> ' . $row['user_name'] . '<br />'; echo '<strong>Date:</strong> ' . $row['date'] . '</td>'; if (is_file(GW_UPLOADPATH . $row['image_name']) && filesize(GW_UPLOADPATH . $row['image_name']) > 0) { echo '<td valign="top" width = 700px height = 600px>'; $vote_image_name = $row['image_name']; require('vote.php'); echo '<A HREF= "' . GW_UPLOADPATH . $row['image_name'] . '"><center><img style="border:2px solid black" width = "500px" src="' . GW_UPLOADPATH . $row['image_name'] . '" alt="Image Missing" /></a></center></td></tr>'; } else { echo '<td valign="top" width = 700px height = 600px><center><img style="border:2px solid black" src="' . GW_UPLOADPATH . 'unverified.gif' . '" alt="Unverified image" /></center></td></tr>'; } } echo '</table>'; mysqli_close($dbc); ?> And here is the vote script that gets called in: <?php // already Connected to the database // $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (isset($_POST['submit'])) { $week = CURRENT_WEEK; $vote = mysqli_real_escape_string($dbc, trim($_POST['vote'])); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to MySQL server.'); $query = "INSERT INTO user_votes VALUES (0, '$week', '$vote_image_name', '$vote')"; mysqli_query($dbc, $query); echo '<p>Your vote has been recorded.</p>'; // mysqli_close($dbc); } ?> <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo PIC_MAXFILESIZE; ?>" /> <fieldset> <legend>Vote</legend> <label for="vote">Vote:</label> <input type="radio" id="vote" name="vote" value="1" />1 <input type="radio" id="vote" name="vote" value="2" />2 <input type="radio" id="vote" name="vote" value="3" />3 <input type="radio" id="vote" name="vote" value="4" />4 <input type="radio" id="vote" name="vote" value="5" />5 </fieldset> <input type="submit" value="Vote" name="submit" /> </form> Thanks for any ideas you might have, Craig Link to comment https://forums.phpfreaks.com/topic/208838-voting-system-problem/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.