我有一个简单的数据库,我想通过该数据库更新许多记录。每页有一个记录,用户可以单击以移至第一,下一个,上一个和最后一个记录。分页和更新一样有效。什么不起作用(这使我烦恼)是这样的事实:单击按钮时,它不会返回同一记录,也不会显示最近输入的值。导航然后再次返回表明更新已成功。我确定问题的部分原因在于:
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
我非常感谢您的帮助。这可能很简单,但却使我望而却步。我在下面粘贴完整的代码。谢谢。
<?php
$id = "";
$fname = "";
$lname = "";
$email = "";
$selected="";
if ( isset( $_POST[ 'id' ] ) ) {
$id = $_POST[ 'id' ];
}
if ( isset( $_POST[ 'fname' ] ) ) {
$fname = $_POST[ 'fname' ];
}
if ( isset( $_POST[ 'lname' ] ) ) {
$lname = $_POST[ 'lname' ];
}
if ( isset( $_POST[ 'email' ] ) ) {
$email = $_POST[ 'email' ];
}
if ( isset( $_POST[ 'submit' ] ) ) {
$selected = implode(', ', (array)$_POST['warning']);
}
?>
<?php
$connect = mysqli_connect( "localhost", "", "", "ps10" );
$record_per_page = 1;
$page = '';
if ( isset( $_GET[ 'page' ] ) ) {
$page = $_GET[ 'page' ];
} else {
$page = 1;
}
$start_from = ( $page - 1 ) * $record_per_page;
$query = "SELECT * FROM studentdata LIMIT $start_from, $record_per_page";
$result = mysqli_query( $connect, $query );
?>
<!DOCTYPE html>
<html>
<head>
<title>Pagination</title>
</head>
<body>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<table width='50%' border='1'>
<?php
while ( $row = mysqli_fetch_array( $result ) ) {
?>
<tr>
<td>ID</td>
<td><input name="id" id="id" value="<?php echo $row['id']; ?>" size="40">
</td>
</tr>
<tr>
<td>First Name</td>
<td><input name="fname" type="text" id="fname" value="<?php echo $row['fname']; ?>" size="40">
</td>
</tr>
<tr>
<td>Last Name</td>
<td><input name="lname" type="text" id="lname" value="<?php echo $row['lname']; ?>" size="40">
</td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" type="text" id="email" value="<?php echo $row['email']; ?>" size="40">
</td>
</tr>
<tr>
<td>Warnings</td>
<td><input name="selected" type="text" id="selected" value="<?php echo $row['selected']; ?>" size="120">
</td>
</tr>
<?php
}
?>
</table>
<input type="checkbox" name="warning[]" value="more than 10% is quoted material">More than 10% is quoted material
<br><br>
<input type="checkbox" name="warning[]" value="question not answered">Question not answered
<br><br>
<input type="checkbox" name="warning[]" value="significant plagiarism">Significant plagiarism
<br><br>
<input type="checkbox" name="warning[]" value="more than 10% underlength">More than 10% underlength
<br><br>
<input type="checkbox" name="warning[]" value="possibly not own work">Possibly not own work
<br><br>
<br>
<input name="submit" type="submit" class="button1" value="Update Record">
</td>
</form>
<?php
$sql = "UPDATE studentdata SET fname='$fname', lname='$lname', email='$email', selected='$selected' WHERE id='$id'";
$result = mysqli_query( $connect, $sql );
?>
<p>
<?php
echo "<table width = '50%' border = '0'>";
?>
<?php
$page_query = "SELECT * FROM studentdata ORDER BY id ASC";
$page_result = mysqli_query( $connect, $page_query );
$total_records = mysqli_num_rows( $page_result );
$total_pages = ceil( $total_records / $record_per_page );
$start_loop = $page;
$difference = $total_pages - $page;
if ( $difference <= 2 ) {
$start_loop = $total_pages - 2;
}
$end_loop = $start_loop + 1;
if ( $page > 1 ) {
echo "<tr><td align ='center' width ='25%' ><a href='student_record_pagination_update.php?page=1'>First</a></td>";
echo "<td align ='center' width ='25%'><a href='student_record_pagination_update.php?page=" . ( $page - 1 ) . "'>Previous</a></td>";
}
for ( $i = $start_loop; $i <= $end_loop; $i++ ) {
echo "<a href='student_record_pagination_update.php?page=" . $i . "'></a>";
}
if ( $page <= $end_loop ) {
echo "<td align ='center' width ='25%' ><a href='student_record_pagination_update.php?page=" . ( $page + 1 ) . "'>Next</a></td>";
echo "<td align ='center' width ='25%' ><a href='student_record_pagination_update.php?page=" . $total_pages . "'>Last</a></td>";
}
?>
<?php
echo "</table>";
?>
</body>
</html>