Solved

passign values from database to textbox with php/mysql

Posted on 2007-04-01
4
242 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Download a website to hdd 2 48
myqsl update statement on phpMyAdmin 8 21
Session timeout 5 13
how to handle $x_amount to payment gateway in php - stumped. 14 11
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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
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.

930 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