Solved

passign values from database to textbox with php/mysql

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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

15 Experts available now in Live!

Get 1:1 Help Now