Jump to content

One of my form fields not displaying data


crmamx

Recommended Posts

The ID field doesn't display the data but it must be there because it was the key for the query. It is an int if that makes any difference.

 

<html>
<head>
</head>
<body>
<?php
// Connect to database=====================================================

include("connect_db.php"); 

// retrieve form data ======================================================

$id = $_POST['id']; 

// sending query ===========================================================

$query = "SELECT ama,model_name,model_mfg,wingspan,engine,decibels FROM airplanes WHERE id='$id'";
if( !$result = mysql_query($query) ) {
    echo "<br>Query $query<br>Failed with error: " . mysql_error() . '<br>';
} else {
    $fetch = mysql_fetch_array( $result );
}

// Output form with retrieved data ==========================================
?>
<h3>Change the data and then click the CHANGE button</h3><br>
<form name="Form51" action="update_db_airplane.php" method="post">
ID #:<input type="text" name="id" value="<?=$fetch[id]?>" /><br>
AMA #:<input type="text" name="ama" value="<?=$fetch[ama]?>" /><br>
Model Name:<input type="text" name="model_name" value="<?=$fetch[model_name]?>" /><br>
Model Mfg:<input type="text" name="model_mfg" value="<?=$fetch[model_mfg]?>" /><br>
Wingspan:<input type="text" name="wingspan" value="<?=$fetch[wingspan]?>" /><br>
Engine:<input type="text" name="engine" value="<?=$fetch[engine]?>" /><br>
Decibels:<input type="text" name="decibels" value="<?=$fetch[decibels]?>" /><br><br>
<input name="submit" id="submit" value="CHANGE!" type="submit">
</form>
<br>
<body>
</html>





Link to comment
Share on other sites

Couple things:

First, the short tag syntax, <? and <?= is deprecated, and will end up causing you problems in the future. It would be advisable to always use long <?php open tags, and the full <?php echo rather than <?=

 

If you had error reporting on, you'd see a ton of errors regarding undefined constants. You need to quote array indices when not already enclosed in a quoted string. i.e. <?php echo $fetch['ama']; ?>, otherwise php first looks for them as constants, wasting time and resources before deciding it should treat them as string values.

 

For your original question, you didn't list `id` in the fields that want retrieved in the query, so it won't be present in the $fetch array. Since the query is designed to retrieve only one record, you can either add `id` to the list of fields, or just echo it as $id, since it's already defined.

Link to comment
Share on other sites

Now I am beginning to wonder if I will ever catch on to php. I assumed (I know, never assume anything) that since id was available in the POST command I could echo it. Except, whoops, I am echoing FETCH...damn.

 

Have noted your other two suggestions and will change them in my other 3 programs also.

 

Pikachu, could you please take a look at "Something other than the standard form" posted under HTML Help.

 

Thanks again.

Link to comment
Share on other sites

I see where the error was and made the change but still not displaying id. Once again, id is an integer. Is that it?

 

<html>
<head>
</head>
<body>
<?php
// Connect to database=====================================================

include("connect_db.php"); 

// retrieve form data ======================================================

$id = $_POST['id']; 

// sending query ===========================================================

$query = "SELECT id,ama,model_name,model_mfg,wingspan,engine,decibels FROM airplanes WHERE id='$id'";
if( !$result = mysql_query($query) ) {
    echo "<br>Query $query<br>Failed with error: " . mysql_error() . '<br>';
} else {
    $fetch = mysql_fetch_array( $result );
}

// Output form with retrieved data ==========================================
?>
<h3>Change the data and then click the CHANGE button</h3><br>

<form name="Form51" action="update_db_airplane.php" method="post">
// <?php echo $fetch['ama']; ?>
ID #:<input type="text" name="id" value="<?php echo $fetch['id']; ?>" /><br>
AMA #:<input type="text" name="ama" value="<?php echo $fetch['ama']; ?>" /><br>
Model Name:<input type="text" name="model_name" value="<?php echo $fetch['model_name']; ?>" /><br>
Model Mfg:<input type="text" name="model_mfg" value="<?php echo $fetch['model_mfg']; ?>" /><br>
Wingspan:<input type="text" name="wingspan" value="<?php echo $fetch['wingspan']; ?>" /><br>
Engine:<input type="text" name="engine" value="<?php echo $fetch['engine']; ?>" /><br>
Decibels:<input type="text" name="decibels" value="<?php echo $fetch['decibels']; ?>" /><br><br>
<input name="submit" id="submit" value="CHANGE!" type="submit">
</form>
<body>
</html>





Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.