Jump to content

Forms


Mystixs

Recommended Posts

Hello, I am having trouble with this.First here is my code.

<?session_start();if(!session_is_registered(myusername)){header("location:main_login.php");}?><?php$con = mysql_connect("","","");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("", $con);$result = mysql_query("SELECT * FROM news");while($row = mysql_fetch_array($result))echo '<form action="editnews2.php" method="post">';echo "Title: <input type="text" name="title" value=' . $row['name'] . '/>' . '<br>'";echo 'Date: <input type="text" name="date" /><br>';echo 'Message: <input type="text" name="message" /><br>';echo '<input type="submit" />';echo '</form>'?>

Now my problem is I am wanting the Title field to have a value of whatever 'name' is in the mysql table News. But for some reason it won't work. Whats wrong with my code? Any help is appreciated.

Link to comment
Share on other sites

You're mixing the use of sinqlequots and doublequots:

echo "Title: <input type="text" name="title" value=' . $row['name'] . '/>' . '<br>'";

Use this instead

echo 'Title: <input type="text" name="title" value="' . $row['name'] . '"/>' ."<br />\n";

That should work....

Link to comment
Share on other sites

Odd, it shows the form but the text fields are still blank...Here is my code now:

<?session_start();if(!session_is_registered(myusername)){header("location:main_login.php");}?><?php$con = mysql_connect("","","");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("", $con);$result = mysql_query("SELECT * FROM news");while($row = mysql_fetch_array($result))echo '<form action="editnews2.php" method="post">';echo 'Title: <input type="text" name="title" value="' . $row['name'] . '"/>' ."<br />";echo 'Date: <input type="text" name="date" value="' . $row['date'] . '"/>' ."<br />";echo 'Message: <input type="text" name="message" value="' . $row['message'] . '"/>' ."<br />";echo '<input type="submit" />';echo '</form>'?>

Link to comment
Share on other sites

I'm going to have to suggest that your problem is here:

while($row = mysql_fetch_array($result))echo '<form action="editnews2.php" method="post">';echo 'Title: <input type="text" name="title" value="' . $row['name'] . '"/>' ."<br />";echo 'Date: <input type="text" name="date" value="' . $row['date'] . '"/>' ."<br />";echo 'Message: <input type="text" name="message" value="' . $row['message'] . '"/>' ."<br />";echo '<input type="submit" />';echo '</form>'

From my understanding of PHP, that code will loop through the results of your query and execute the following code for each iteration of the loop:

echo '<form action="editnews2.php" method="post">';

Then, once the loop is complete, the remaining echo statements will be processed. If you want each of those echo statements to be processed every time the loop executes, you need to wrap them in curly braces:

while($row = mysql_fetch_array($result)){echo '<form action="editnews2.php" method="post">';echo 'Title: <input type="text" name="title" value="' . $row['name'] . '"/>' ."<br />";echo 'Date: <input type="text" name="date" value="' . $row['date'] . '"/>' ."<br />";echo 'Message: <input type="text" name="message" value="' . $row['message'] . '"/>' ."<br />";echo '<input type="submit" />';echo '</form>';}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...