ohboy21
ohboy21

Reputation: 4319

PHP - Set value in HTML-Form

I'm a PHP-Newbie and want to create a simple Website to manage my CD Collection.

I connect to a MySQL-Database and create for each table a PHP-Array.

No i want to be able to edit my CDs within HTML-Forms and submit it back to the MySQL-Database.

For that i create a for-loop and put each array-varaible into an HTML-Form, so i can edit the text und via submit UPDATE my Database.

I know there is an answer for my question: stackoverflow Answer

But this solution didn't work for me.

One line:

echo "<input name="Titel" type="text" size="30" value="<?php echo $alben[$i]['Titel']; ?>">";   

This line doesn't work, an i don't understand why.

Can someone please help?

EDIT: If i just say:

echo $alben[$i]['Titel'];   

This work. But with the HTML-Form i'm getting this error message:

 syntax error, unexpected T_STRING, expecting ',' or ';'

Upvotes: 0

Views: 10307

Answers (2)

Bruno Nardini
Bruno Nardini

Reputation: 461

if you are in php tag use this:

<?php
...
echo '<input name="Titel" type="text" size="30" value="'. $alben[$i]['Titel']; .'">"';
..
?>

if you are in html, use this:

<input name="Titel" type="text" size="30" value="<?php echo $alben[$i]['Titel']; ?>">

Upvotes: 1

RonPringadi
RonPringadi

Reputation: 1494

Because you need escape the quotes and remove the 2nd echo

echo "<input name="Titel" type="text" size="30" value="<?php echo $alben[$i]['Titel']; ?>">";

into

echo "<input name=\"Titel\" type=\"text\" size=\"30\" value=\"" . $alben[$i]['Titel'] . "\">";

period is string concatination "a" . "b" becomes "ab"

Upvotes: 1

Related Questions