hi,

this is a snippet of my codes,

<td><a href="editFunction.php?ID=<?php echo $contact; ?>">update</a></td>
after clicking "update", the ID will be passed to the editFunction.php page.

editFunction.php

<?php 
		//get variables from previous page
		$ID = $_GET['ID'];
		
		
		//sql statement to retrieve the commands 
		$sql = "SELECT * FROM contacts WHERE ID LIKE '".$ID."'";
		
		//execute query 
		$result = mysql_query($sql) or die (mysql_error());
		
		//display on table 
		$contact = mysql_num_rows($result);  {
?> 
.
......
<td width="140"><span class="style2">ID : </span></td>
    <td width="384">
	  <input name="ID" type="ID" id="ID" value="<? echo $_GET['ID'] ?>" size="50" readonly />        </td>
  </tr>
  <tr>
    <td width="140"><span class="style2">First Name : </span></td>
    <td width="384">
      <input name="firstName" type="text" id="firstName" value="<? echo $contact['firstName'] ?>" size="50" />        </td>
  </tr>
</table>

<?php 
			
            }
?>
</body>
</html>

why aren't my results displaying out in the respective fields except for ID?
where do i start debugging? Thanks alot.

Dani AI

Generated

The core issue was that the script showed the passed ID but never actually placed the queried row into the form fields. As noted, treating the query result as a row count will leave the row data empty. Other pitfalls visible in the original post that commonly mask the same symptom: using short PHP tags (<?) when they may be disabled, and using an invalid input type (for example type="ID" is not a valid HTML input type).

Quick checklist for debugging and a safer fix:

  • Confirm the SQL actually ran and returned a row (enable exceptions or check error output).
  • Validate and cast the incoming ID (e.g. to an integer) before using it in a query.
  • Fetch a single row into an associative array and use that array when filling form values.
  • Output form values with htmlspecialchars(..., ENT_QUOTES, 'UTF-8') to avoid HTML injection.
  • Use WHERE ID = ? (or a prepared parameter) rather than LIKE for an exact ID match.
  • Prefer PDO or mysqli with prepared statements (the old ext/mysql was removed from modern PHP).

Example (modern, minimal PDO pattern) — fetch one row and safely echo a field into an input:

$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4','user','pass',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$stmt = $pdo->prepare('SELECT * FROM contacts WHERE ID = ? LIMIT 1');
$stmt->execute([(int)$_GET['ID']]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

// in HTML:
<input type="text" name="firstName" value="<?php echo htmlspecialchars($row['firstName'] ?? '', ENT_QUOTES, 'UTF-8'); ?>" />

Useful references: PDO prepared statements, fetching rows with PDO, htmlspecialchars, short PHP tags notes (PHP tags), and a note that the old mysql extension was removed (PHP 7 migration). OP () later confirmed the change resolved the problem.

Recommended Answers

All 5 Replies

because contact contains the number of rows returned by the query.

change:

$contact = mysql_num_rows($result);

to

$contact = mysql_fetch_assoc($result);

using this wll help you.
actually mysql_assoc is used to retrive resuts form

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "Name :{$row['name']} <br>" .
         "Subject : {$row['subject']} <br>" . 
         "Message : {$row['message']} <br><br>";
}

i don't think he needs the while loop because he is updating a specific row.

i use mysql_fetch_assoc() all the time. works perfectly. you can also use mysql_fetch_array, they complish the same thing.

use mysql_fetch_array ..........
for mysql_num_rows........

ah, thanks alot. PROBLEM SOLVED :)

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.