Displaying data in text fields..help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Displaying data in text fields..help

Post by crazytopu »

A simple problem..

i want to show the values of a table in a html form containing a few text fields so that i can edit any value already saved in the table and save the new values back.

Code: Select all

<?php


$db = mysql_connect("localhost", "root"); 
mysql_select_db("library",$db) or die(mysql_error()); 
$result = mysql_query("SELECT * FROM book",$db); 

?> 

<tr>
<td> <input type="text" name="book_name" </td>
<td> <input type="text" name="author" </td>
</tr>

<?php 
while ($myrow = mysql_fetch_row($result)) 
{ 
   echo '<tr>'; 
   for($i=0; $i<10; $i++) 
   { 
      echo '<td>'.$myrow[$i].'</td>'; 
   } 
   echo '</tr>'; 
} 

?>

I m just not really sure how to display the data into a text field..What am i missing here in the code? can anybody help?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Displaying data in text fields..help

Post by TheBentinel.com »

I think you're looking for something like this:

(assumes you have fields in your book table named book_name and author)

Code: Select all

<?php


$db = mysql_connect("localhost", "root"); 
mysql_select_db("library",$db) or die(mysql_error()); 
$result = mysql_query("SELECT * FROM book",$db); 

while ($myrow = mysql_fetch_row($result)) 
{ 
   echo '<tr>'; 
   echo '<td> <input type="text" name="book_name" value="' . $myrow["book_name"] . '"</td>';
   echo '<td> <input type="text" name="author" value="' . $myrow["author"] . '"</td>';
   echo '</tr>'; 
} 

?>

Did you want the user to be able to update all the books in the database on one page? Or were you looking to do them one at a time?

Or have I missed your point entirely?
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

yes..i want the librian to update one book at a time..so he will first use a key value to match the exact book he wants to update..and then display the value in a number of text fields (as many attributes are there in the table) and once he brings changes and hits the update button..it gets saved.

i tried your code..but i do get only the blank text field and no data...any clue why is it like that?

yes book_name and author are the attributes in my book table ..and i want to display them in two text fields..
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

And better would be for me to use a html form containing a list of text fields [provided i know exactly how many numbers of attrtibutes are there in the table conerned].

I just want PHP code to take care of extracting the data from the table and put them into corresponding text fields...

Any help appreciated..
Buddha Joe
Forum Newbie
Posts: 6
Joined: Mon Mar 15, 2004 12:38 pm

Displaying text in text fields

Post by Buddha Joe »

Here is a link to a tutorial which I think may help you.

http://www.keithjbrown.co.uk/vworks/php/php_p5.php

I think this covers what you are trying to do.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Displaying text in text fields

Post by TheBentinel.com »

Do you have any records in the book table?

How about printing mysql_num_rows($result) after you get the $result back. Maybe you just aren't getting any data.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

Yes..i can get result using mysql_num_rows($result) as how many rows of i have in my table -"book"..but when i try to display one row into the text fields..using exactly the same code that u gave...but i just get two empty text fields..!

My book table has exactly two columns and 1 row...so..aparently i got some data there..
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

crazytopu wrote:Yes..i can get result using mysql_num_rows($result) as how many rows of i have in my table -"book"..but when i try to display one row into the text fields..using exactly the same code that u gave...but i just get two empty text fields..!

My book table has exactly two columns and 1 row...so..aparently i got some data there..
How about just trying to print the data to the screen without the complication of the input fields, just to make sure that part is working. And get rid of that "author" stuff I sent you, the array may not be indexed on names like that. Looking at the docs, I think I was wrong about that.

Code: Select all

$db = mysql_connect("localhost", "root"); 
mysql_select_db("library",$db) or die(mysql_error()); 
$result = mysql_query("SELECT * FROM book",$db); 

while ($myrow = mysql_fetch_row($result)) 
{ 
   echo $myrow[0] . '<br>'; 
   echo $myrow[1] . '<br>'; 
   echo '<hr>'; 
}
If that works, then add the HTML code around it.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

okay, i got upto this point where i can display data into text fields extracted from a table.

But still this is not what i exactly wanted ..you see PHP is displaying the data in the text field..which i didnot want. I wanted my PHP only to extract data and insert it into the text field..My HTML form would do the rest...but here in this code my PHP is echoing the data into text fields..

Code: Select all

<?php
<?php 

$db = mysql_connect("localhost", "root"); 
mysql_select_db("library",$db) or die(mysql_error()); 
$result = mysql_query("SELECT * FROM book1",$db); 
?>

 

<?php

while ($myrow = mysql_fetch_row($result)) 
{ 

for($i=0; $i<2; $i++) 
{ 
      
  
echo '<td> <input type="text" name="book_name" value="' . $myrow[$i] . '"</td>'; 
echo '<br>';
}

} 

?> 


?>

And i wanted something like

Code: Select all

<?php

<?php 

$db = mysql_connect("localhost", "root"); 
mysql_select_db("library",$db) or die(mysql_error()); 
$result = mysql_query("SELECT * FROM book1",$db); 
?>

 
  

<table border="0" width="75%"> 
  <tr> 
    <td width="28%">Book Call No</td> 
    <td width="49%"><input type="text" name="call_no" size="20"></td> 
  </tr> 
  <tr> 
    <td width="28%">Member ID</td> 
    <td width="49%"><input type="text" name="mem_id" size="20"></td> 
  </tr> 
</table> 

 

<?php

while ($myrow = mysql_fetch_row($result)) 
{ 

for($i=0; $i<2; $i++) 
{ 
      
  
 <input type="text" name="book_name" value="' . $myrow[$i] . '"</td>';  


 /*Does this code insert the value into the text field that already exist in my HTML form? 
 It is something different  from the way i did it in the above example ..i mean creating a set of text fields inside my php
 code. What i simply want is ..i want a HTML coding that layouts my text fields  and everything inside a HTML
form  and i want PHP code simply to extract data and assign the data against each respective text field and display 
them.*/ 
 
 
  //and here i want to display the value..
 
 }

} 

?> 


?>
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

wow... now i'm really confused as to what your asking for... mind trying and re-asking that question again??
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

oops! :roll:

sorry if my last post was too confusing..ok here i go with a simpler view so that you guys get it easy to understand.

I have a php file called insertbook.php that i used to insertdata. I want to add a new button (named "modify" or "update") in the same insertbook form. So, when the user click "update" button i want to take user input for the book_call_no that they want to modify. Now upon getting the book_call_no [primary key of the book table].. i want to extract that row from the table and display it in the insertbook.php file.

I wonder if that's possible? Can i use the same form for two different purpose? Onec to send data and once to retrieve data and display it?


I would be very grateful if somebody can give me a sample code ....well i really appreciate the weblink Buddah gave me..i am gonna take a look there now...
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

Okay, I'm going to give this a shot. What it sounds like you have is one page with fields that are empty to start with for inserting a new book, however if you want to update a book you want the librarian to just be able to type in the call number for the book, click update and have the fields fill in? Since PHP is server-side, you will have to post to the same page again and have two different sets of html based on wether or not the book_call_no typed in exists in the database.

Here I go:

Code: Select all

<?php

$db = mysql_connect("localhost", "root");
mysql_select_db("library",$db) or die(mysql_error());

$book_call_no = $_POST['book_call_no'];  //on the form post, get values
$book_name = $_POST['book_name'];
$author = $_POST['author'];


if (!empty($book_call_no)) {  //librarian typed in a call no
$result = mysql_query("SELECT * FROM book1 WHERE book_call_no = $book_call_no",$db); 

$myrow = mysql_fetch_row($result);

if (!empty($myrow)) { //found a book with the call no
echo '<tr>';
    echo '<td> <input type="text" name="book_call_no" value="' . $myrow[0] . '"</td>';
   echo '<td> <input type="text" name="book_name" value="' . $myrow[1] . ''"</td>';
   echo '<td> <input type="text" name="author" value="' . $myrow[2] . '"</td>';
   echo '</tr>'; 

} else {   //no book with the call number, must be new, lets insert
   mysql_query("INSERT INTO book1 SET ($book_call_no, $book_name, $author)");
}

} else { //nothin in any of the fields, the form hasn't been posted yet
?>
<tr>
<td> <input type="text" name="book_call_no" </td>
<td> <input type="text" name="book_name" </td>
<td> <input type="text" name="author" </td>
</tr> 
<?
}

?>

I wouldn't try that code exactly, because I'm sure there are errors, but it should be close enough to get you where you're trying to go.
Post Reply