我在wordpress工作。您将看到两个查询,一个是select查询,另一个是insert查询。我希望在运行插入查询后,在页面上获得更新的结果。
选择查询有一个带有提交按钮的表单标记。。。提交时,它将拾取id或当前获取的行,并在另一个表中以相同的id执行插入。但在此之后,单击“提交我的页面”不会显示数据库中的更新值或插入值。我希望你们能在这方面提出建议。。。谢谢
wordpress页面模板中的PHP代码
$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 />\';
}
最合适的回答,由SO网友:Rene Korss 整理而成
移动insert
查询到文件顶部。因此,它将选择更新的数据。
<?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>";
}
?>