Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating forms with mySQL data

Status
Not open for further replies.

Gazzieh

IS-IT--Management
Dec 18, 2006
117
GB
Hi

I am experimenting with mySQL and PHP and now a bit lost. I have the data in a mySQL database and have integrated FCKeditor correctly. But now I simply want to have a textbox above the FCKeditor window with the database field topic's contents displayed. If the topic changes then I need the system to update the database.

Code:
<?php

	// Connect to the database
	$cnx = mysql_connect("localhost", "root", "password")
	OR die("Unable to connect to database!");
	mysql_select_db("database", $cnx);
?>
		 
<?php
	if ($_POST['submit_form'] == 1) 
		{
		// Save to the database
		$details = mysql_real_escape_string(trim($_POST['fcktext']));
		$result = mysql_query("UPDATE instructions SET details = '".$details."' WHERE id = 1"); 
		mysql_query("UPDATE instructions SET udate = NOW() WHERE id = 1");
		mysql_query("UPDATE instructions SET utime = NOW() WHERE id = 1");
		if (!$result)
			die("Error saving the record!  Mysql said: ".mysql_error());
		
		// Redirect to self to get rid of the POST
		header("Location: editor.php");
		}
?>

<?php include_once("fckeditor/fckeditor.php") ;?>
<?php @ require_once("header.php"); ?>

		<title>Edit Pages</title>
	</head>
	<body>
		<div id="main">
			<form action="editor.php" method="post">
				
				<?php
					// Get data from the database
					$query = mysql_query("SELECT * FROM instructions WHERE id = 1");
					$data = mysql_fetch_array($query);
					
					// Configure and output editor
					$oFCKeditor = new FCKeditor('fcktext');
					$oFCKeditor->BasePath = '[URL unfurl="true"]http://localhost/garynewport/designs/fckeditor/';[/URL]
					$oFCKeditor->Value    = $data["details"];
	  				$oFCKeditor->Height   = 400;
					$oFCKeditor->Create() ;
				?>
				<br>
				
				<input type="hidden" name="submit_form" value="1" />
				<input type="submit" value="Save Form" />
			</form>
		</div>
		<?php
  			mysql_close($cnx);
		?>
	</body>
</html>

Anyone able to help?
 
before this line
Code:
 // Configure and output editor
add the following
Code:
echo <<<HTML
<textarea name="topic">{$data['topic']}</textarea>
HTML;

and change these lines
Code:
        $result = mysql_query("UPDATE instructions SET details = '".$details."' WHERE id = 1"); 
        mysql_query("UPDATE instructions SET udate = NOW() WHERE id = 1");
        mysql_query("UPDATE instructions SET utime = NOW() WHERE id = 1");

to
Code:
$topic = mysql_real_escape_string(trim($_POST['topic']));
$result = mysql_query("UPDATE instructions SET details='$details', topic='$topic', date=now(), utime=now() WHERE id = 1");
 
Thanks for this. It works excellently and allows me to move forward.

The line that has me thrown a bit is this one:

Code:
echo <<<HTML
<textarea name="topic">{$data['topic']}</textarea>
HTML;

Not only because it does not look anything like the echo commands I have used before but also because when I try to format it (tab it in, combine it onto a single line), the page fails to load properly. Why would this be?
 
Its called heredoc syntax, and it needs to be that way.

It opens the echo statement so that you can just write out anything you need in it without worrying about quotes or anything like that. It also has the added benefit of evaluating any variables in it.

However the closing tag, in this case [red]HTML;[/red] needs to be on a line of its one, without anything before it not even a space.

PHP's online manual has a section detailing it, just scroll down a bit to find it.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top