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??

Ankur
  • 269
  • 2
  • 4
  • 15

6 Answers6

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()

Community
  • 1
  • 1
Halcyon
  • 57,230
  • 10
  • 89
  • 128
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: http://it1.php.net/htmlentities

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'];?>">
rebyte
  • 70
  • 1
  • 4
0

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

'$r["incharge"])'

change it to

"$r['incharge'])"
CS GO
  • 914
  • 6
  • 20
0

try,

value = "<?php echo $r['incharge']; ?>" 
Jaykishan
  • 1,409
  • 1
  • 15
  • 26
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

Dileep Kumar
  • 247
  • 1
  • 10