1

I am working in wordpress. You will see two queries one is select and the other is insert query. I want that after my insert query is run i get updated results on my page.

The select query has a form tag with submit button ... which when submitted picks the id or current fetched row and performs an insert at the same id in another table. But after this click on submit my page does not show the updated value or inserted value from database. I hope you people can advise in this ... thanks.

PHP code in wordpress page template

        $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";

$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

Move insert query to top of file. So it will select updated data.

<?php
// Save form data
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 />';
}

// Get already updated results
$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>";

}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.