passign values from database to textbox with php/mysql

Posted on 2007-04-01
Last Modified: 2013-12-12
I am trying to build an update page,
I have problem in getting the vaules from the database into a textboxes so the user can see the old values,
it is my first time to do this so I don't know how:
This is my code:


$query=" SELECT * FROM Companies WHERE Company_id='1'";
while ($i < $num) {




<title>Update Page</title>

<form action="updated.php" method="post">

Compnay_id <input type="text" name="ud_first" value="<? echo $Company_id; ?>"><br>
Company_name <input type="text" name="ud_last" value="<? echo $Company_name; ?>"><br>
City <input type="text" name="ud_phone" value="<? echo $City; ?>"><br>
State <input type="text" name="ud_mobile" value="<? echo $State; ?>"><br>
<input type="Submit" value="Update">

This did not work with me and don't stuck with this code, if you have better ideas or easier code it would be great,

Thank you

Question by:amer_net
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 35

Expert Comment

ID: 18833524
The problem is that mysql_numrows should have an additonal _ and that to pull rhte result values you need to use something like $row = mysql_fetch_array or mysql_fetch_assoc with the $result,

also you may want to use full tags for your echoing into your text values

Author Comment

ID: 18833595
can you give me any example
forget about my code because I took it from the internet and i did not understand it very well,
I want any example in which I can take the values from my database into the textbox,
use only one text box thats fine.

thank you
LVL 35

Accepted Solution

Raynard7 earned 500 total points
ID: 18833688
Hi, I have unashamedly modified the excellent example for mysql_fetch_assoc from the php site,
note that this should retreive your data put them into values then output - I have not tested the form but the idea is there
now - if you do not have short tags enabled <? may not work - so I have used <?= which may be used as a shortcut for <?php echo the others use the unabbreviated form - which may be the only one that works.


$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
if (!mysql_select_db("mydbname")) {
    echo "Unable to select mydbname: " . mysql_error();

$sql = "SELECT * FROM Companies WHERE Company_id='1'";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    $Company_id = $row["Company_id"];
    $Company_name = $row["Company_name"];
      $City = $row["City"];
      $State = $row["State"];





<title>Update Page</title>

<form action="updated.php" method="post">

Compnay_id <input type="text" name="ud_first" value="<?=$Company_id; ?>"><br>
Company_name <input type="text" name="ud_last" value="<?php echo $Company_name; ?>"><br>
City <input type="text" name="ud_phone" value="<?php echo $City; ?>"><br>
State <input type="text" name="ud_mobile" value="<?php echo $State; ?>"><br>
<input type="Submit" value="Update">

Author Comment

ID: 18833715
Thank you so much

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question