若要将某些内容插入数据库,您应该在post请求中发送数据,从请求中获取此数据并保存它。您创建的代码将在post请求时插入循环的最后一行。
您应该为之前的每次旅行创建表单,并添加隐藏的输入字段,以便有机会在post请求时获取它们。
<?php
// Handling request and saving to database should not be in template,
// but for the simplicity of the example I will leave it here.
if( isset( $_POST[\'rebook\'] ) ) {
// Get value from $_POST array secure way after sanitization
$destination = filter_input( INPUT_POST, \'destination\', FILTER_SANITIZE_STRING );
$departing = filter_input( INPUT_POST, \'departing\', FILTER_SANITIZE_STRING );
$table = $wpdb->prefix . "table_name";
$wpdb->insert(
$table,
array(
\'your_destination\' => $destination,
\'your_departing\' => $departing
)
);
}
?>
<?php $rows = array(); // Get data from database ?>
<?php foreach ( $rows as $row ): // Iterate over data ?>
<?php
// Create form for each previous trip to that
// there would be an opportunity to save each
?>
<form method="post" action="">
<div> Previous Trip from:
<?php echo $row->your_departing .\' to : \'.$row->your_destination; ?>
<?php // Create hidden fields to send \'departing\' and \'destination\' with $_POST data ?>
<input type="hidden" name="departing" value="<?php esc_attr( $row->your_departing ); ?>">
<input type="hidden" name="destination" value="<?php esc_attr( $row->your_destination ); ?>">
<button type="submit" name="rebook" class="signupbtn">REBOOK</button>
</div>
</form>
<?php endforeach; ?>