user187680
user187680

Reputation: 673

Display a form within a php loop

I am using a simple loop that looks like this:

$query = "SELECT * FROM $username"; 
$result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
        echo $row['id']. " - ". $row['file'];
        echo "<br />";
                echo "<form method="post" action="" style="width: 80px">
            <input name="Checkbox1" type="checkbox" /><input name="Submit1" type="submit"               value="submit" /></form>";

When i run it like this I get an error that the < is unexpected. I believe I may be doing something entirely wrong. Is there some other approach that would output a table within a php loop.

Upvotes: 1

Views: 2205

Answers (5)

KoU_warch
KoU_warch

Reputation: 2150

You cannot use double quotes (") for specify strings in a double quote statement echo "<div id="pong" >; without the backslash \". You have three choices:

  1. Change your first and last " for '
  2. Put backslashes in your code.
  3. Concatenate string like: echo "<form method="."post".">"

Upvotes: 0

PachinSV
PachinSV

Reputation: 3780

You should scape your double quotes or just use sigle quotes for the echo statement.

Upvotes: 0

iDev247
iDev247

Reputation: 1901

You have quotes within quotes. They should be escaped using \.

echo "<form method=\"post\" action=\"\" style=\"width: 80px\">...";

You can also use single quotes:

echo '<form method="post" action="" style="width: 80px">...';

The difference between single and double quotes is that single quotes does not show variables:

<?php
$a = 'b';
echo '$a'; // output: $a
echo "$a"; // output: b
echo $a; // output b

Upvotes: 4

zessx
zessx

Reputation: 68790

I don't know if it's the only problem, but you must escape your double quotes :

echo "<form method=\"post\" action=\"\" style=\"width: 80px\">
        <input name=\"Checkbox1\" type=\"checkbox\" />
        <input name=\"Submit1\" type=\"submit\" value=\"submit\" />
      </form>";

You can also use simple quote to delimite your string :

echo '<form method="post" action="" style="width: 80px">
        <input name="Checkbox1" type="checkbox" />
        <input name="Submit1" type="submit" value="submit" />
      </form>';

Upvotes: 1

Quentin
Quentin

Reputation: 943564

See this line:

echo "<form method="post" action="" style="width: 80px">
     ^             ^
     |             End of string
     Start of string

Escape quotes (\") inside strings delimited with the same type of quotes.

Upvotes: 7

Related Questions