While displaying the data from the database on a text box, I can access only the first part of the name. For example, if the name is 'Five Star', only the name 'Five' is displayed. Can you help in this? I'm just a beginner.
This is the code i've used echo "<tr><td>Reg.No.:</td><td width='200'><input type='text' name='reg' value=".$row[RegNo]."></td></tr>";

Dani AI

Generated

You ran into two separate issues the thread hints at. First, as and @diafol noted, HTML attribute values with spaces must be quoted, or the browser will stop at the first space. Second, make sure you are outputting a specific field, not the whole row array. Using $row[RegNo] without quotes relies on PHP’s legacy "undefined constant" behavior; use $row['RegNo'] or $row['name'] explicitly.

For a robust pattern, always HTML-escape the value before inserting it into an attribute, so names like O'Connor or Alice "Ace" Ng do not break the markup. This also prevents accidental XSS if the data contains special characters.

<?php
// Safely read the field (empty string if missing)
$name = isset($row['name']) ? (string)$row['name'] : '';

// Escape for HTML attribute context
$attr = htmlspecialchars($name, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');

// Output with quotes around the attribute
printf('<input type="text" name="name" value="%s">', $attr);

If things still look odd, debug exactly what you have before rendering: var_dump($row); and var_dump($row['name']); as suggested. If you see "Array" or NULL, you are not referencing the correct key or you are echoing the whole result set instead of a single row. Also view the page source in the browser to confirm the rendered HTML shows a properly quoted value="...". Finally, avoid mixing multiple quoting styles inside one echo; either interleave PHP in HTML, or build the line with printf/sprintf to keep quoting clear and error-free.

Recommended Answers

All 19 Replies

How does the text look like in the database? Five Star? or Five_Star?

Member Avatar for Member #120589

Names need to be single words. No spaces. When you say 'name', I'm assuming you mean the name attribute. If not, what do you mean?

echo 1st the $row[RegNo] to see the value, because it could be that the $row[RegNo] only contains the "five" text

Hello,
please check below code:

<?php
$reg=$row[RegNo];

echo "<tr><td>Reg.No.:</td><td width='200'><input type='text' name='reg' value='$reg'></td></tr>";
?>

substr function in PHP and space please !

commented: Do not hijack a thread, start your own. -3

If the value attribute is used without quotes, then the space (after Five in your case) will be assumed as the separator for the next attribute.

<!-- this was your output -->
<input type='text' name='reg' value=Five Stars />

<!-- this is what it should be (as amitengg showed) -->
<input type='text' name='reg' value='Five Stars' />
commented: Ah! Now I get it. Yes, agree. +15

How does the text look like in the database? Five Star? or Five_Star?

It is Five Star. Din't use any underscore.

Names need to be single words. No spaces. When you say 'name', I'm assuming you mean the name attribute. If not, what do you mean?

Why can't I use two words? Is there any way I can do this?
<input type="text" name="name" value=<?php echo $name; ?>><br/> hear it is the php code but i can't access full name it only print the first part of the name can u help me pls? but i can access full name from $name in outside the input label

Member Avatar for Member #120589

Sorry I misread your first post. Your value attribute can pretty much be anything

You haven't added the quotes yet:

<input type="text" name="name" value="<?php echo $name; ?>"><br/>

If the value attribute is used without quotes, then the space (after Five in your case) will be assumed as the separator for the next attribute.

<!-- this was your output -->
<input type='text' name='reg' value=Five Stars />

<!-- this is what it should be (as amitengg showed) -->
<input type='text' name='reg' value='Five Stars' />

I've used quotes for the value attribute. But it is the same.

Show your new code then.

You haven't added the quotes yet:

<input type="text" name="name" value="<?php echo $name; ?>"><br/>

Student Name:<input type='text' name='name' value=".$row.">"

$name = end(explode(" ", $row['name']));  // Star
$name = explode(" ", $row['name']));  // Five
Member Avatar for Member #120589

Are you using php to output the whole html?

either

<input type="text" name="name" value="<?php echo $row['name']; ?>"><br/>

OR

echo "<input type=\"text\" name=\"name\" value=\"{$row['name']}\"><br/>";

Post full code or a block of your code that contains the above line.

Post full code or a block of your code that contains the above line.

echo "<tr><td>Student Name:</td><td width='200'><input type='text' name='name' value=".$row."></td></tr>";

You are STILL missing the quotes around the value... Look again at @ardav's code.

echo "<tr><td>Student Name:</td><td width='200'><input type='text' name='name' value=".$row."></td></tr>";

Thank u so much everyone. I got the solution.

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.