0

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>
4
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by tiggsy
0

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>';
}
0

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>';
    }
}

Edited by P0lT10n: n/a

0

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

0

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>

Edited by Stefano Mtangoo: n/a

0

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>
0

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

0

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

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

0
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
0
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
0

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

Edited by tiggsy: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.