简体   繁体   中英

Filling form with editable value - php and html

I have read a lot of question here but I couldn't get anything to work. I have such a instructions:

$query = "SELECT nazwa,rok_prod,wypornosc FROM statek where id_statek=$id";
$wynik = pg_query($query);
$liczba_kolumn = pg_num_fields($wynik);
echo "<form action=edos.php method=post>";
echo "<table border width=1>";
for($k = 0;$k<$liczba_kolumn; $k++)
{
    echo "<tr>";
    echo "<td>";
    echo pg_field_name($wynik,$k);
    echo "</td>";
    echo "<td>";
    echo "<input type=text name=".pg_field_name($wynik,$k) "value=".pg_fetch_result($wynik,0,$k).">";
    echo "</td>";
    echo "</tr>";
}
echo "</table>";

And I want to display values from SELECT in fields, that I could change it later - it is for editing form. I have tried in a lot of ways:

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

or

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

but nothing is working. I have tested pg_fetch_result($wynik,0,$k) and there is what I want but how to display it and make it editable?

First, I recommend you to read about PHP Strings , especially the difference between single and double quoted strings and how to escape characters.

Second, you're forgetting to add the double quotes around the HTML attribute values, forgot a dot and am missing a space. This line:

echo "<input type=text name=".pg_field_name($wynik,$k) "value=".pg_fetch_result($wynik,0,$k).">";

Would output something like <input type=text name=foovalue=bar> , assuming that your first function call will return foo and the second returns bar .

What you need is, for example:

echo "<input type=\"text\" name=\"".pg_field_name($wynik,$k)."\" value=\"".pg_fetch_result($wynik,0,$k)."\">";

Which should output <input type="text" name="foo" value="bar">

As this tends to get messy and is therefore error-prone, I would recommend you to look into parsing strings with functions like printf .

Apart from that, I am guessing that your functions pf_fetch_name and pg_fetch_result are not returning anything (or an empty string), and therefore you get empty input fields. Hence, the error might lie within these function and/or the SQL queries they are (probably?) carrying out. This is what you should look into.

Edit :

To get things a bit tidier, I would further recommend to avoid all the echo s. This can be done by simply having the actual HTML markup outside of the <?php ?> tags and then injecting your values with the shorthand tags <?= ?> . A shortened example:

<?php
// Some PHP code
for ($i = 1; $i <= $something; ++$i) {
?>
<table>
    <tr>
        <td><?= fetch_foo(); ?></td>
        <td><?= fetch_bar(); ?></td>
    </tr>
<table>
<?php
} // closing the for loop
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM