0

i am setting the value of a few textboxes using the data from the database as shown.

$qry="select * from branch where id=4";
$res=$con->query($qry);
$r=$res->fetch_assoc();

Actually i am printing the table with the textbox inside the echo statement as below:

echo '
<td>Incharge:</td><td><input type="text" name="incharge" size="38" value='.'$r["incharge"])'.'></td>';

But the problem is that it is only printing the first string in the textbox.for e.g.if the value of $r["incharge"] fetched is Ankur Lakhani, but it is only printing Ankur. Any solution??

2
  • you are missing the quotes : value='<?=$r["incharge"]?>' Commented Mar 3, 2014 at 12:56
  • 1
    ... value="'.$r["incharge"].'" ... Commented Mar 3, 2014 at 13:09

6 Answers 6

3

Your output HTML looks like:

<input type="text" name="incharge" value=Ankur Lakhani>

as you can tell by the highlighting, it's not interpreted the way you expect.

It is interpreted as:

<input type="text" name="incharge" value="Ankur" Lakhani="Lakhani">

Add quotes to ensure the whole value is used:

<input type="text" name="incharge" value="Ankur Lakhani">

You should also add escaping incase your string contains HTML sensitive characters like " and >

<input type="text" name="incharge" value="<?php echo htmlspecialchars($r["incharge"]); ?>"/>

Relevant: htmlentities() vs. htmlspecialchars()

Sign up to request clarification or add additional context in comments.

Comments

1

When you output data as value of a text input you should escape it, using the htmlentites function :

<input type="text" name="incharge" value="<?php echo htmlentities($r["incharge"]); ?>"/>

Reference: https://www.php.net/htmlentities

Comments

0

you forgot setting quotes "" for your value.

try this:

<?php
$qry="select * from branch where id=4";
$res=$con->query($qry);
$r=$res->fetch_assoc();
?>
<input type="text" name="incharge" value="<?php echo $r['incharge'];?>">

Comments

0

try,

value = "<?php echo $r['incharge']; ?>" 

Comments

0

If the problem is in your client side then it will this one

'$r["incharge"])'

change it to

"$r['incharge'])"

Comments

0

Try this

<input type="text" name="incharge" value="<?= $r['incharge'];?>">

or use like <?= htmlspecialchars($r['incharge'])?>

Must Enable your Short_open_tag on your config file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.