Solved: echo query results in a form

Hello all. First post here and not much experience with PHP. I am trying to create a blog for a customer of mine and am having some issues with some of my script. When a writer logs in, I have a page that displays all of the stories they have written. From there, I have a link to edit a story. The edit story page is where I’m having problems. I would like for it to display what they have already written so that they do not have to re-type the whole story. However, my results are not being displayed. I’m not sure where the problem lies. The only thing I can get to return back is the story id. Perhaps someone could point me in the right direction?

PHP for edit_story.php

[code]<?php
include (‘include_fns.php’);

if (isset($_REQUEST['story'])){

$conn = mysql_connect("localhost", "*********", "******") 
or die($msg_no_connect);
mysql_select_db("content") 
or die(mysql_error());

// Run query
$sql = “SELECT * FROM stories WHERE writer=’{$_SESSION[‘auth_user’]}’”;
$res = mysql_query($sql);
if(!$res){
$err=mysql_error();
echo $err;
exit();
}
}
else {
echo ‘No results to display.’;
}

?>[/code]

form to update story where results should be echoed:

[code]

			<tr>
				<td>Headline<td>
			</tr>
			<tr>
				<td><input size="80" name="headline" value="<?php echo $res['headline']; ?>"></td>
			</tr>

			<tr>
				<td>Page</td>
			</tr>
			<tr>
				<td><select name="page" id="page">
					<option value="beauty">Beauty</option>
					<option value="business">Business</option>
					<option value="destinations">Destinations</option>
					<option value="fashion">Fashion</option>
					<option value="health">Health & Fitness</option>
					<option value="home">Home & Gourmet</option>
					<option value="women">Women of the World</option>
					<option value="relationships">Relationships</option>
					<option value="entrepreneur">Entrepreneur</option>
					<option value="spirit">Spirit & Balance</option>
					<option value="workplace">The Workplace</option>
					</select>
				</td>
			</tr>

			<tr>
				<td>Story text (can contain HTML tags)</td>
			</tr>
			<tr>
				<td><textarea cols="80" rows="7" name="story_text" wrap="virtual"><?php echo $res['story_text']; ?></textarea></td>
			</tr>
			<tr>
				<td><input type="submit" value="Submit"></td>
			</tr>

			</table>
			</form>[/code]

As I said, the value for story id returns correctly, but the subsequent results like the headline are not returned. I have tried changing the $res to $_REQUEST and still nothing is returned. What am I missing?

Thanks so much for any help or suggestions!

Hi,

after running the code:

// Run query $sql = "SELECT * FROM stories WHERE writer='{$_SESSION['auth_user']}'"; $res = mysql_query($sql); if(!$res){ $err=mysql_error(); echo $err; exit(); }

$res contains a result resource returned from the mysql query. You still need to fetch the values from this resource:

$row = mysql_fetch_assoc($res) echo $row['story_text'];

will get you the first rows value from the field ‘story_text’.

Try running that code after you submit the query and see if you get the results you are looking for.

Hi, waylon999. That is pretty much what I did and was able to get my results. I went a round about way of doing it so will try the way you suggested for some cleaner code. Thanks very much for your reply!

Sponsor our Newsletter | Privacy Policy | Terms of Service