我正在尝试从数据表中检索数据,并且在每个检索到的行旁边都会有一个更新按钮,如果单击更新按钮,它将仅更新该特定行。为此,我尝试了以下代码,但它只需单击即可更新所有数据记录。如何仅更新特定行?
<?php
global $wpdb;
$retrieve_data = $wpdb->get_results( "SELECT * FROM wp_share" );
echo '<table border=1 class="table table-striped">
<tr><th>Id</th><th>purchasedate</th><th>purchasevalue</th>
<th>offervalue</th><th>monthlyearnings</th><th>status</th></tr>';
foreach ($retrieve_data as $retrieved_data){
echo '<tr><td>'. $retrieved_data->id.'</td>';
echo '<td>'.$retrieved_data->purchasedate.'</td>';
echo '<td>'.$retrieved_data->purchasevalue.'</td>';
echo '<td>'. $retrieved_data->offervalue.'</td>';
echo '<td>'. $retrieved_data->monthlyearnings.'</td>';
echo '<td>'. $retrieved_data->status.'</td>';
?>
<form method="post" enctype="multipart/form-data">
<td><input type="submit" name="update" value="update" /></td>
</form>
<?php
$myid= $retrieved_data->id;
if (isset($_POST['update'])) {
//global $wpdb;
?><script>//alert('<?php// echo $myid; ?>');</script><?php
$wpdb->update('wp_share', array( 'Status' => 'pay'),array('id'=>$myid));
}
}?>
答案 0 :(得分:2)
请使用以下代码。
<?php global $wpdb;
$retrieve_data = $wpdb->get_results( "SELECT * FROM wp_posts" );
echo '<table border=1 class="table table-striped">
<tr><th>Id</th><th>purchasedate</th><th>purchasevalue</th>
<th>offervalue</th> <th>status</th></tr>';
foreach ($retrieve_data as $retrieved_data){
echo '<tr><td>'. $retrieved_data->ID.'</td>';
echo '<td>'.$retrieved_data->post_author.'</td>';
echo '<td>'.$retrieved_data->post_date.'</td>';
echo '<td>'. $retrieved_data->post_title.'</td>';
echo '<td>'. $retrieved_data->comment_status.'</td>';
?>
<form method="post" enctype="multipart/form-data">
<td>
<input type="hidden" name="pid" value= "<?php echo $retrieved_data->ID; ?>">
<input type="submit" name="update" value="update" /></td>
</form>
<?php
if (isset($_POST['update'])) {
$myid = $_POST['pid'];
global $wpdb;
?> <?php
$wpdb->update('wp_posts', array( 'comment_status' => 'pay'),array('id'=>$myid));
}
}?>