update page after insert query
-
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
[ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]
$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' >" . ' '; echo "<input name='id' type='hidden' value='$result->uid'>"; echo "<input name='comp' type='hidden' value='$result->competition'>"; echo $result->username.''; echo $result->votessum.''; 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 ''.'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 />';
The topic ‘update page after insert query’ is closed to new replies.