Puzzledplane
Puzzledplane

Reputation: 105

Populating Text Field with Data from Mysql Database with PHP

I am trying to populate a text field with data from a MySQL database, but my code seems to not be working.

Any help would be greatly appreciated.

Heres the code:

<?php include ("../config.php");

// Connect to server and select database.
mysql_select_db($db, $con);

// get value of id that sent from address bar
$id = $_GET['ID'];


// Retrieve data from database
$sql="SELECT * FROM members WHERE id='$id'";
$result=mysql_query($sql);

echo '<form name="Edit" method="post" action="update.php">

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Name:</strong></td>
<td align="center"><strong>Contact:</strong></td>
<td align="center"><strong>Division:</strong></td>
<td align="center"><strong>Rank:</strong></td>
</tr>';
            while ($row = mysql_fetch_assoc($result))
            {
            //we will echo these into the proper fields

        echo '<tr>';
        echo '<td align="center"><input name="Name" type="text" id="Name" value="'; echo $row['Name']; echo '"></td>';
        echo '<td align="center"><input name="Contact" type="text" id="Contact" value="'; echo $row["Contact"]; echo'" size="15"></td>';
        echo'<td>
        <select name="Divison">
        <option value="L4D2">L4D2</option>
        <option value="CSS">CSS</option>
        <option value="GMOD">GMOD</option>
        <option value="TF2">TF2</option>
        </select>
        </td>
        <td>
        <select name="Rank">
        <option value="Division Leader">Division Leader</option>
        <option value="Admin">Administrator</option>
        <option value="Member">Member</option>
        </select>
        </td>
        </tr>';


echo '<tr>';
        echo '<td align="center"><input type="submit" name="Submit" value="Submit"></td>
        </tr>
</table>
</td>
</form>';
}
echo '
</tr>
</table>
</body>
</html>';


// close connection
mysql_close();

?>

Upvotes: 0

Views: 8790

Answers (5)

thegaffney
thegaffney

Reputation: 440

From previous comments, the id in your url is lower case change $_GET['ID'] to $_GET['id']

Im I allowed to do this? Keeps other people with problem from having to read all the comments

New to this site, thanks!

Upvotes: 0

Rahul Kanodia
Rahul Kanodia

Reputation: 105

This code:

$sql="SELECT * FROM members WHERE id='$id'";

should be like this

$sql="SELECT * FROM members WHERE id='{$id}'";

Also use

$result=mysql_query($sql) or die(mysql_error());

For debugging purpose.

Remove mysql_close() and it should work

Upvotes: 0

Vaibhav Gupta
Vaibhav Gupta

Reputation: 1612

edited my answer with full fixes:

<?php include ("../config.php");

// Connect to server and select database.
mysql_select_db($db, $con);

// get value of id that sent from address bar
$id = $_GET['ID'];

// Retrieve data from database
$sql="SELECT * FROM members WHERE id='$id'";
$result=mysql_query($sql);

echo '<form name="Edit" method="post" action="update.php">
            <table width="400" border="0" cellspacing="1" cellpadding="0">
                <tr>
                    <td align="center"><strong>Name:</strong></td>
                    <td align="center"><strong>Contact:</strong></td>
                    <td align="center"><strong>Division:</strong></td>
                    <td align="center"><strong>Rank:</strong></td>
                </tr>';
while ($row = mysql_fetch_assoc($result)) {
    //we will echo these into the proper fields

    echo '<tr>
                <td align="center"><input name="Name" type="text" id="Name" value="'.$row['Name'].'"></td>
                <td align="center"><input name="Contact" type="text" id="Contact" value="'.$row["Contact"].'" size="15"></td>
                <td>
                    <select name="Divison">
                        <option value="L4D2">L4D2</option>
                        <option value="CSS">CSS</option>
                        <option value="GMOD">GMOD</option>
                        <option value="TF2">TF2</option>
                    </select>
                </td>
                <td>
                    <select name="Rank">
                        <option value="Division Leader">Division Leader</option>
                        <option value="Admin">Administrator</option>
                        <option value="Member">Member</option>
                    </select>
                </td>
            </tr>';
}
echo     '<tr>
                <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
            </tr>
           </table>
        </form>';

// close connection
mysql_close();
?>

now replace whole coding with this one and reply...??

Upvotes: 1

Jay
Jay

Reputation: 1

Make sure you use a querystring with a legitimate ID like example.com?ID=1. If you are posting a form and the post method for the form is post, you should use $_POST['ID'] instead of $_GET['ID'].

Upvotes: 0

Bruce
Bruce

Reputation: 1542

Just a guess but I think your column names in the db would be all lower case. But in your php you have $row["Contact"]. I think it should be $row["contact"]

Upvotes: 0

Related Questions