Hello I am working on wordpress and below is my php code. I show images by fetching their path from database and have a vote button with every image and when it is clicked the vote is added to the votes table. After vote is added I want to show that queue of images again with updated votes for all the images. I am doing this successfully but when I press vote button for the second time to add second vote I get a message that database insertion failed. Below is my code and snapshot of how my page looks and what is the error are all mentioned below. Images are comming from articles table which has 1:N relationship with votes table.
PHP Code
if(isset($_POST['submit'])){
$ccc = $_POST['comp'];
$cat =$_POST['category'];
global $wpdb;
$compp = $wpdb->get_var("SELECT competition FROM competition ORDER BY cid DESC LIMIT 1");
$userid = $_POST['id'];
$myvote = 1;
if($wpdb->insert(
'zvotes',
array(
'zvotes' => $myvote,
'zcompetition' => $compp,
'aid' => $userid
)
) == false) wp_die('Database Insertion failed'); else echo 'your vote was successfully recorded';
//show the updated results
//get current competition value
$sqll = "SELECT articles.aid, articles.username, articles.competition, articles.path, articles.category, articles.title, Sum(zvotes.zvotes) AS votessum FROM articles LEFT JOIN zvotes on articles.aid=zvotes.aid WHERE articles.category = '$cat' && articles.competition = '$ccc' GROUP BY articles.aid, articles.username, articles.competition, articles.path, articles.category, articles.title ORDER BY votessum";
$results = $wpdb->get_results($wpdb->prepare($sqll)) or die(mysql_error());
foreach( $results as $result ) {
echo '<form action="" method="post">';
echo "<input name='category' type='hidden' value='$result->category'>";
echo $result->title.'<br>';
echo "<img src='$result->path' width='150' height='150' >" . '<br><br>';
echo $result->body.'<br>';
echo "<input name='comp' type='hidden' value='$result->competition'>";
echo $result->username.'<br>';
echo $result->votessum.'<br>';
echo "<input style='margin-bottom:30px;' value='vote' name='submit' type='submit'/></form>";
}//end of foreach
}
/////////////////////////////////////////////////////////////////////////
// drop down
echo '<form action="" method="post">';
echo '<select name="category" id="category" style="width:250px; background-color:lightgrey;">';
echo '<option value="" disabled="disabled" selected="selected" ">Select category</option>';
echo '<option value="My Testimony">My Testimony</option>';
echo '<option value="Love & Relationships">Love & Relationships</option>';
echo '<option value="Miscellaneous">Miscellaneous</option>';
echo '</select>';
echo '<input type="submit" name="a" value="Search" style="margin-left:15px; margin-bottom:15px;">';
echo '</form>';
//show after drop down value is selected
if(isset($_POST['a'])){
//echo "zeeshanaslamdurrani". "<br>";
echo do_shortcode('[ujicountdown id="Photos Contest" expire="2015/04/30 00:00" hide="true" url="" subscr="sdf" recurring="" rectype="second" repeats=""]');
global $wpdb;
//get current competition value
$cat =$_POST['category'];
$comp = $wpdb->get_var("SELECT competition FROM competition ORDER BY cid DESC LIMIT 1");
//echo $comp;
$comp = $wpdb->get_var("SELECT competition FROM competition ORDER BY cid DESC LIMIT 1");
echo "current competition is ". $comp;
$sqll = "SELECT articles.aid, articles.username, articles.competition, articles.path, articles.category, articles.title, Sum(zvotes.zvotes) AS votessum FROM articles LEFT JOIN zvotes on articles.aid=zvotes.aid WHERE articles.category = '$cat' && articles.competition = '$comp' GROUP BY articles.aid, articles.username, articles.competition, articles.path, articles.category, articles.title ORDER BY votessum";
$results = $wpdb->get_results($wpdb->prepare($sqll)) or die(mysql_error());
foreach( $results as $result ) {
echo '<form action="" method="post">';
echo "<input name='category' type='hidden' value='$result->category'>";
echo "<input name='id' type='hidden' value='$result->aid'>";
echo $result->title.'<br>';
echo "<img src='$result->path' width='150' height='150' >" . '<br><br>';
echo $result->body.'<br>';
echo "<input name='comp' type='hidden' value='$result->competition'>";
echo $result->username.'<br>';
echo $result->votessum.'<br>';
echo "<input style='margin-bottom:30px;' value='vote' name='submit' type='submit'/></form>";
}//end of foreach
}//end of isset
My page
After vote(which is a form submit button) is pressed once
Error on second pressing vote button second time after page refresh
votes table
Articles table (articles table has 1 to many relationship with votes table)