doujing5846 2015-04-20 06:21
浏览 89

表单提交在第二次提交时失败

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 enter image description here

After vote(which is a form submit button) is pressed once enter image description here

Error on second pressing vote button second time after page refresh enter image description here

votes table enter image description here

Articles table (articles table has 1 to many relationship with votes table) enter image description here

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)