formatting data from mysql - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

formatting data from mysql

entoento Posts: 1Member
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 |
| |

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 |
Last Name |

I've been playing around with the code and trying to add s here and there but I can't get it. At best, it looks like this:

First Name
Last Name

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

body {font-family:arial;}
.error {font-weight:bold; color:#FF0000;}

Edit Contact Records

Edit Contact Records:

//connect to the database
$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
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 = '

' . $firstname . '
' . $lastname . '

// 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 = '


// 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 = '

// end 'if record exists' if
//end 'if ID given in URL' if
// end 'if form posted' if

// close connection

// print error/success message
echo (IsSet($msg)) ? "
" : "";

<? echo (IsSet($form_start)) ? $form_start : "";

" />

<? echo (IsSet($table_row)) ? $table_row : "";

<? echo (IsSet($form_end)) ? $form_end : ""; ?>
First Name Last Name

Back to customer list


Sign In or Register to comment.