Reputation: 3
i'm trying to select values from the database and place each and every one on a separate text input field. my code works properly, however, when i tried to display a string on a text field, the string is cut or trimmed on its very first space character. for example:
value #1: "my-picture.jpg"
value #2: "my name"
if i were to place the two values above and insert them inside a text field, the output would be like this:
value #1: my-picture.jpg
value #2: my
this is the code i'm working on:
<?php
$counter = 0;
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
echo "
<input type='text' value=".$newArray['my_picture'].">
<input type='text' value=".$newArray['my_name'].">
";
?>
What is wrong? Thanks for any help.
Upvotes: 0
Views: 935
Reputation: 18859
Try viewing the source.. That says:
<input type='text' value=the content of your variable>
Where 'the content of your variable' needs to be surrounded by quotes, naturally. So; change it into:
<?php
$counter = 0;
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
echo '<input type="text" value="' . $newArray['my_picture'] . '">';
echo '<input type="text" value="' . $newArray['my_name'] . '">';
}
?>
Upvotes: 2
Reputation: 11375
You need to surround your value with quotes. Something like
echo "
<input type='text' value='".$newArray['my_picture']."'>
<input type='text' value='".$newArray['my_name']."'>
";
Upvotes: 1
Reputation: 21553
You need to quote your values in your HTML form:
echo '
<input type="text" value="'.$newArray['my_picture'].'">
<input type="text" value="'.$newArray['my_name'].'">
';
Also for good measure it would be a good idea to use htmlentities()
or htmlspecialchars()
on the values in your variables to encode special characters:
echo '<input type="text" value="' . htmlentities($newArray['my_picture']) . '">
<input type="text" value="' . htmlentities($newArray['my_name']) . '">';
Upvotes: 7