好吧,我爲你做了個開始。我想你想列出來自數據庫的所有行到一個HTML表格中,並在底部添加一個表格,以便將新行添加到數據庫中,對吧?
如果是這樣,那麼你可以用這個單一的PHP頁面代碼來做到這一點。請注意,我沒有考慮安全查詢的任何安全性!你可以在互聯網上找到更多關於這個的信息。
<?php
$DB_NAME = 'DATABASE_NAME';
$DB_HOST = 'DATABASE_HOST';
$DB_USER = 'DATABASE_USER';
$DB_PASS = 'DATABASE_PASSWORD';
// CONNECT TO THE DATABASE
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//form submit?
if(isset($_POST['save'])) {
//INSERT QUERY
$q = "INSERT INTO `your_table`(`field1`, `field2`, `field3`) VALUES ('" . $_POST['field1'] . "', '" . $_POST['field2'] . "', '" . $_POST['field3'] . "')";
$mysqli->query($q) or die($mysqli->error.__LINE__);
}
//get all items from db (also the new row)
$query = "SELECT * FROM `your_table`";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
//build first part of table
echo '<table>
<thead>
<th>
<td>Your field1</td>
<td>Your field2</td>
<td>Your field3</td>
</th>
</thead>
<tbody>';
//create rows
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<tr>
<td>' . $row['field1'] . '</td>
<td>' . $row['field2'] . '</td>
<td>' . $row['field3'] . '</td>
</tr>';
}
} else {
echo '<tr><td colspan=3>NO RESULTS</td></tr>';
}
//close table
echo ' </tbody>
</table>';
// CLOSE CONNECTION
mysqli_close($mysqli);
?>
<!-- the form -->
<form action="#" method="POST">
<input type="text" name="field1" />
<input type="text" name="field2" />
<input type="text" name="field3" />
<input type="submit" name="save" value="save" />
</form>
希望這有助於!
請粘貼您的代碼。 – eisberg
我已經讀過你的問題了,你的意思是:我在表單中編輯行,提交,然後表單必須填充下一行的數據? –