Jump to content

php forms and database navigatio


andriano

Recommended Posts

Hello,

I'm new to php and i'd like to post the following.

I have written code to get records from a DB and i need one record at a time to be filled in a form i created. Then the next record should be displayed after the user hits the "press any ket button". It does not work

 

PLEASE HELP !

 

code is following

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 

 

<?php

 

$labels = array(  "user_name" => "User Name", "create_date" => "Creation Date", "password" => "Password",

"last_name" => "Last Name",

"first_name" => "First Name",

"street" => "Street",

"city" => "City",

"state" => "State",

"zip" => "Zip",

"email" => "Email",

"phone" => "Phone",

"fax" => "Fax");

 

$submit = "Press any key";

include("dbstuff.inc");

$cxn = mysqli_connect($host,$user,$passwd,$databname);

if (!cxn) {

printf("Can't connect to localhost. Errorcode: %d\n", mysqli_connect_errno ());

}

else  { echo "connection successful !";

}

/*  or  die ("could not connect to server");*/

$query = "SELECT * FROM customer ";

$result = mysqli_query ($cxn,$query) or die ("could not execute query");

/*$customer = mysqli_fetch_assoc($result);*/

 

?>

 

<html>

<head><title>Customer Record</title>

<style type='text/css'>

 

/*div {background-color:#b0c4de}*/

body {color:blue}

body {background-color:#b0c4de}

 

<!--

#form {margin: 1.5em 0 0 0;

padding: 15;

}

#field {padding-bottom: 1em;}

label {

font-weight: bold;

float: left;

width: 10%;

margin-right: 1em;

text-align: right;

}

  -->

</style>

 

</head>

<body>

<h3> Customer Details</h3>

 

<?php

/* Loop that displays the form fields */

/* print_r ($customer);*/

 

 

while ($customer = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

 

echo "<form action = '$_SERVER[php_SELF]' method = 'POST'>

<div id='form'>";

 

foreach($labels as $field => $label)

{

echo "<div id='field'><label for='$field'>$label</label>

 

<input id='$field' name='$field' type='text'

size='50%' maxlength='65' value = {$customer[$field]} />

             

</div>\n";

 

}              /* foreach*/

echo "<input style='margin-left: 33%' type='submit' value='$submit' />\n";

 

  }  /*while*/

  /*echo "</div>\n";*/

 

 

/* free result set */

  mysqli_free_result($result);

/* close connection */

mysqli_close($cxn);

 

?>

</form></body></html>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/178582-php-forms-and-database-navigatio/
Share on other sites

This would just show the last record in the database.

 

There are a few problems with this script.

 

Firstly you are re-building the form with each loop. I'm quessing that if anything, it is only displaying one record. Most likely the last one in the database. This is because you have wrapped the form in a div with an ID. As far as I'm aware you can only have one instance of an ID per page. I'm willing to bet if you are getting any results and you remove the ID's or change them to classes you'll have one page with a list of forms one for each record. There are a few ways to get what you want but I'd suggest looking into pagination. there are a number of tutorials on it if you google it.http://www.google.co.uk/search?hl=en&rlz=1T4ADSA_enGB333GB333&q=pagination+in+PHP&meta=&aq=f&oq=

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.