你好我正在使用wordpress,下面是我的php代码。我通过从数据库中获取路径来显示图像,并且每个图像都有一个投票按钮,当点击它时,投票会被添加到投票表中。在添加投票后,我想再次使用所有图像的更新投票显示该图像队列。我成功地做了这个,但是当我第二次按下投票按钮添加第二次投票时,我收到一条数据库插入失败的消息。下面是我的代码和我的页面看起来如何以及错误的快照都在下面提到。图像来自于与表格具有1:N关系的文章表格。
PHP代码
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
我的页面
投票后(表格提交按钮)按一次
页面刷新后第二次按下第二次按下投票按钮时出错
投票表
文章表(文章表与投票表有1对多的关系)