MySQL插入不再有效

时间:2015-04-09 05:54:01

标签: php mysql wordpress

我在wordpress工作,下面是我的代码,它在for循环和提交按钮中有一个表单标记。单击提交按钮时,需要执行插入操作,但插入功能不再起作用。可能是它无法选择ID ...非常感谢帮助...

PHP代码

$sql = "SELECT 1user.username, 1user.competition, 1user.path, Sum(votes.votes) AS votessum FROM 1user LEFT JOIN votes on 1user.uid=votes.uid GROUP BY 1user.username, 1user.competition";
$results = $wpdb->get_results($sql) or die(mysql_error());

foreach( $results as $result ) {
    echo '<form action="" method="post">';
    echo "<img src='$result->path' width='150' height='150' >" . '<br><br>';
    echo "<input name='id' type='hidden' value='$result->uid'>";
    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>";        
}

if(isset($_POST['submit'])){
    global $wpdb;
    $votes = 1;
    $competition = $_POST['comp'];
    $uid = $_POST['id'];
    //$uid = get_current_user_id(); 

    echo 'id of image = '.$_POST['id'];
    echo '<br>'.'competition is'.$_POST['comp'];
    if($wpdb->insert(
            'votes',
            array(
                    'votes' => $votes,
                    'competition' => $competition,
                    'uid' => $uid
                )
            ) == false) 
        wp_die('Database Insertion failed'); 
    else 
        echo 'Database insertion successful<p />';
}

1 个答案:

答案 0 :(得分:0)

是的,它解决了,发现选择查询中缺少解决方案1user.uid字段..谢谢大家

更正选择查询

$sql = "SELECT 1user.uid, 1user.username, 1user.competition, 1user.path, Sum(votes.votes) AS votessum FROM 1user LEFT JOIN votes on 1user.uid=votes.uid GROUP BY 1user.username, 1user.competition";