Solved

passign values from database to textbox with php/mysql

Posted on 2007-04-01
4
251 Views
Last Modified: 2013-12-12
Hi,
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:

<?php
$username="whatever";
$password="whatever";
$database="whatever";
mysql_connect(localhost,$username,$password);

$query=" SELECT * FROM Companies WHERE Company_id='1'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$Company_id=mysql_result($result,$i,"Company_id");
$Company_name=mysql_result($result,$i,"Company_name");
$City=mysql_result($result,$i,"City");
$State=mysql_result($result,$i,"State");

++$i;
}
?>

<html>

<head>

<title>Update Page</title>
</head>

<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">
</form>
</html>

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


0
Comment
Question by:amer_net
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Raynard7
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
0
 

Author Comment

by:amer_net
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
0
 
LVL 35

Accepted Solution

by:
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.

<?php

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

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

$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();
    exit;
}

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

// 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"];
}

mysql_free_result($result);

?>

<html>

<head>

<title>Update Page</title>
</head>

<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">
</form>
</html>
0
 

Author Comment

by:amer_net
ID: 18833715
Thank you so much
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now