Solved

passign values from database to textbox with php/mysql

Posted on 2007-04-01
4
270 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
[X]
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
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

729 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