I am trying to get my database to display in a form using this code below. The problem is, the form (textfield) is not displaying anything. Can someone give me a hint?

<?php
session_start();  
 
if (!isset($_SESSION['memberusername'])){  
	header("Location: contractorlogin.php");  
	exit();  
}
 
mysql_connect('localhost','xxx','xxxxxx') or die( mysql_error() );
mysql_select_db('xxxxx') or die( mysql_error() );
$user = mysql_real_escape_string($_SESSION['memberusername']);
$sql = "SELECT Username FROM contractors WHERE Username='" . $user . "'";
$result = mysql_query($sql) or die( 'Unable to execute<br>'.$sql.'<br>'.mysql_error());
if( !mysql_num_rows($result) )
{
	echo '<p>No Records found!</p>';
}
else
{
	$row=mysql_fetch_assoc($result);
	echo '<table><tr><th>' .implode('</th><th>', array_keys($row) ).'</th></tr>';
	do
	{
		echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
	}while($row=mysql_fetch_assoc($result));
	echo '</table>';
}
 
?>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>
 
<body>
<form action="<?php $_SERVER['PHP_SELF']?> " method="post" enctype="multipart/form-data">
<input type="text" name="myname" value="<?php echo $row['name']?>">
<input type="submit" name="submit" value="Update">
</form>
</body>
</html>

Recommended Answers

All 10 Replies

calling $row=mysql_fetch_assoc($result) twice :icon_eek:

else
{
    $row=mysql_fetch_assoc($result);
    echo '<table><tr><th>' .implode('</th><th>', array_keys($row) ).'</th></tr>';
    do
    {
        echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
    }while($row=mysql_fetch_assoc($result));
    echo '</table>';
}
Member Avatar for P0lT10n

Wrong code...

else
{
    $row=mysql_fetch_assoc($result);
    echo '<table><tr><th>' .implode('</th><th>', array_keys($row) ).'</th></tr>';
    do
    {
        echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
    }while($row=mysql_fetch_assoc($result));
    echo '</table>';
}

You wrote

while($row=mysql_fetch_assoc($result));

fist of all, it's

while(condition){ /*repeat something until it's false*/ }

Right code...

else
{
    while($row = mysql_fetch_assoc($result)){
        echo '<table><tr><th>' .implode('</th><th>', array_keys($row) ).'</th></tr>';
        echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
        echo '</table>';
    }
}

Wrong code...

else
{
    $row=mysql_fetch_assoc($result);
    echo '<table><tr><th>' .implode('</th><th>', array_keys($row) ).'</th></tr>';
    do
    {
        echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
    }while($row=mysql_fetch_assoc($result));
    echo '</table>';
}

You wrote

while($row=mysql_fetch_assoc($result));

fist of all, it's

while(condition){ /*repeat something until it's false*/ }

Right code...

else
{
    while($row = mysql_fetch_assoc($result)){
        echo '<table><tr><th>' .implode('</th><th>', array_keys($row) ).'</th></tr>';
        echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
        echo '</table>';
    }
}

He is using do...while

This code works, modify to your needs.

<?php 
$server='localhost';
        $username='xxxx';
        $password='xxx';
        $database_name='xxxx';
        $conn = mysql_connect($server, $username, $password)or die("Cannot connect");
        mysql_select_db($database_name, $conn)or die("Cannot Select given database");

        $sql = "SELECT employee_number, full_name FROM user";
        $result = mysql_query($sql);
   ?>
<html>
    <body>
        <form>
            <?php
               while($rows=  mysql_fetch_array($result)){
                   echo "<input type='text' name=$rows[0] value=$rows[1] /> <br />";
               }
            ?>
        </form>
    </body>
</html>

The code works, but the incorrect data is not being displayed in the form. In the form, I a getting the character '/', and that is all. I have a column called fname that has names stored. The name is not showing up at all.
Here's what I am using for my code:

<?php 
session_start();  
 
if (!isset($_SESSION['memberusername'])){  
	header("Location: contractorlogin.php");  
	exit();  
}

mysql_connect('localhost','name','pword') or die( mysql_error() );
mysql_select_db('databasename') or die( mysql_error() );
$user = mysql_real_escape_string($_SESSION['memberusername']);
$sql = "SELECT Username FROM contractors WHERE Username='" . $user . "'";
$result = mysql_query($sql) or die( 'Unable to execute<br>'.$sql.'<br>'.mysql_error());
	   ?>
	<html>
	    <body>
	        <form>
            <?php
               while($rows=  mysql_fetch_array($result)){
                   echo "<input type='text' name=$rows[0] value=$rows[1] /> <br />";
}
?>
</form>
</body>
</html>

The code works, but the incorrect data is not being displayed in the form. In the form, I a getting the character '/', and that is all. I have a column called fname that has names stored. The name is not showing up at all.
Here's what I am using for my code:

<?php 
session_start();  
 
if (!isset($_SESSION['memberusername'])){  
	header("Location: contractorlogin.php");  
	exit();  
}

mysql_connect('localhost','name','pword') or die( mysql_error() );
mysql_select_db('databasename') or die( mysql_error() );
$user = mysql_real_escape_string($_SESSION['memberusername']);
$sql = "SELECT Username FROM contractors WHERE Username='" . $user . "'";
$result = mysql_query($sql) or die( 'Unable to execute<br>'.$sql.'<br>'.mysql_error());
	   ?>
	<html>
	    <body>
	        <form>
            <?php
               while($rows=  mysql_fetch_array($result)){
                   echo "<input type='text' name=$rows[0] value=$rows[1] /> <br />";
}
?>
</form>
</body>
</html>

Post your minimal DB schema and minimal values in db

There are 4 fields for now since this is a test..

UserID : tinyint, autoincrement
Username: text
Password: text
fname: text

echo "<input type='text' name=$rows[0] value=$rows[0] /> <br />";//UserID 
 echo "<input type='text' name=$rows[1] value=$rows[1] /> <br />";//Username
 echo "<input type='text' name=$rows[2] value=$rows[2] /> <br />";//Password
 echo "<input type='text' name=$rows[3] value=$rows[3] /> <br />";//fname
echo "<input type='text' name=$rows[0] value=$rows[0] /> <br />";//UserID 
 echo "<input type='text' name=$rows[1] value=$rows[1] /> <br />";//Username
 echo "<input type='text' name=$rows[2] value=$rows[2] /> <br />";//Password
 echo "<input type='text' name=$rows[3] value=$rows[3] /> <br />";//fname

if you want to use an array variable in an echo statement you need to put it in curly brackets: eg.

echo "<input type='text' name='{$rows[0]}' value='{$rows[0]}' /> <br />";//UserID
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.