I'm trying to modify code from a book called "PHP with MySQL" where data is read from a MySQL db, edited and written back. It's all technically working but the data is written to the screen from left to right as follows (where the top row is the heading and the second row is the data retrieved from the database in an edit box.):
First Name | Last Name |
<firstname> | <lastname> |
I have tons of fields, so the headings scroll left to right on the screen and I find it really annoying. I'd like things to go top-to-bottom so it looks like this:
First Name | <firstname>
Last Name | <lastname>
I've been playing around with the code and trying to add <tr>s here and there but I can't get it. At best, it looks like this:
First Name
Last Name
<firstname>
<lastname>
I'm sure it's something simple, but I just can't see it.
Any help would be appreciated... the full code is below
# cat editrecord.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style>
body {font-family:arial;}
.error {font-weight:bold; color:#FF0000;}
</style>
<title>Edit Contact Records</title>
</head>
<body>
<h2>Edit Contact Records:</h2>
<?
//connect to the database
include("dbinfo.php");
$link = mysqli_connect($server, $username, $password, $database);
// has the form been submitted?
if ($_POST) {
foreach($_POST as $k => $v) {
$v = trim($v);
$$k = $v;
}
// build UPDATE query
$update = "UPDATE contact_records SET
firstname='$firstname',
lastname='$lastname'
WHERE Id=$id";
// execute query and check for success
if (!mysqli_query($link, $update)) {
$msg = "Error updating data";
} else {
$msg = "Record successfully updated:";
// write table row confirming data
$table_row = '
<tr>
<td>' . $firstname . '</td>
<td>' . $lastname . '</td>
</tr>';
}
// if not posted, check that an Id has been
// passed via the URL
} else {
if (!IsSet($_GET['id'])) {
$msg = "No customer selected!";
} else {
$id = $_GET['id'];
//build and execute the query
$select = "SELECT firstname, lastname FROM contact_records where id=$id";
$result = mysqli_query($link, $select);
// check that the record exists
if (mysqli_num_rows($result)<1) {
$msg = "No customer with that ID found!";
} else {
// set vars for form code
$form_start = "<form method=\"post\"action=\"" . $_SERVER['PHP_SELF'] . "\">";
$form_end = '
<tr>
<td colspan="2"><input type="submit" value="Submit changes" /></td>
<td colspan="3"><input type="reset" value="Cancel" /></td>
</tr>
</form>';
// assign the results to an array
while ($row = mysqli_fetch_array($result)) {
$firstname = $row['firstname'];
$lastname = $row['lastname'];
// write table row with form fields
$table_row = '
<tr>
<td><input type="text" name="firstname" value="' . $firstname . '" size="10" /></td>
<td><input type="text" name="lastname" value="' . $lastname . '" size="10" /></td>
</tr>';
}
// end 'if record exists' if
}
//end 'if ID given in URL' if
}
// end 'if form posted' if
}
// close connection
mysqli_close($link);
// print error/success message
echo (IsSet($msg)) ? "<div class=\"error\">$msg</div>" : "";
?>
<table border="1" cellpadding="5">
<!-- Show start-of-form code if form needed -->
<? echo (IsSet($form_start)) ? $form_start : "";
?>
<input type="hidden" name="id" value="<? echo $id ?>" />
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<!-- Show appropriate table row code (none set if there were errors) -->
<? echo (IsSet($table_row)) ? $table_row : "";
?>
<!-- Show end-of-form code if we are displaying the form -->
<? echo (IsSet($form_end)) ? $form_end : ""; ?>
</table>
<br /><a href="records.php">Back to customer list</a>
</body>
</html>