Page 1 of 1

Newbie in need of help databas Output not echoing properly??

Posted: Tue Sep 05, 2006 2:15 pm
by EvoDan
Hi guys I have been trying to set up a simple Content management system for a client using tinymce as the main editor form, but as i'm a total newbie and have just started learning php its proving quite difficult for me. What i have managed to do so far seems to be ok except the output on my public page echoed from my database isnt working right at all. So i was hoping somone here can take a look at my scripts and tell me whats right and wrong
so here is what i have so far....

I created a database with phpmyadmin called "boarhunt" and a table named "news" with two fields these are set like so..

1. id, int, (11), not null, primary key.
2.content, text, not null, default.

So it looks like this...
http://img521.imageshack.us/img521/2966/dbshotdi1.jpg

For my form page i have tinymce setup with the form action set to post to postnews.php like so...

Code: Select all

<form action="postnews.php" method="post">
		  <div id="elm1" style="width:450px; height:250px">
		  
		  
	</div>
<input type="submit" value="Submit">
    </form>
There is also some javascript in the head of the page to create the wysiwyg editor in the page where the id elm1 appears.

Here is my code for the postnews.php file...

Code: Select all

<?
$username="******";
$password="******";
$database="boarhunt";
$host="localhost";

if(isset($_POST['save']))

$content=$_POST['content'];


mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

{
$query = "insert into News (id,content) values ('','$content')"; 
mysql_query($query) or die(mysql_error('Error Query Failed'));

echo htmlspecialchars($query);
}


mysql_close();
?>
This seem all well and good as it seems to be posting to the database ok as everytime i submit something it adds rows to the database
and it increases in size so i assume that part is working, but if there is anything odd about it please let me know?


Next (and this is where i have hit a brick wall) i have tried to write an output script to go on my public pages to display the edited content
but all i get back is a bunch of numbers.

here is the script for my public page...

Code: Select all

<?
$host="localhost";    
$username="******";
$password="******";
$database="boarhunt";



$db = mysql_connect($host,$username,$password);
@mysql_select_db($database) or die("Unable to select database");


$result = mysql_query("SELECT * FROM news");
while($row = mysql_fetch_array($result))
{
echo $row['id'] . " " . $row['content'];
}
@mysql_close($db); 
?>
This is what this code is echoing back from my database

http://img399.imageshack.us/img399/8571 ... putgo0.jpg

So what i would like to know is how do i get the page to display the content i entered into the wysiwyg editor instead of those numbers?
And where am i going wrong with my script?

Many thanks in advance to anybody who can help me out with this as i have been scratching my head over this for a week now and i have gotten as far as i can on my own with this.

Posted: Tue Sep 05, 2006 2:30 pm
by thallish
Hi

just to see what the output of your query is, try to print_r($row) in your while loop

and when your accessing $row by id's anyway take a look at mysql_fetch_assoc()

Posted: Tue Sep 05, 2006 4:33 pm
by EvoDan
Thanks for the quick reply but i tried what you said here and i still seem to get the same output? But as i said i have only been learning php for a week so there could be a possibility i am doing it wrong :cry: .
Any other ideas?

Posted: Wed Sep 06, 2006 1:13 am
by thallish
well it looks like that whatever comes in 'content' is false somehow, try to echo out your $_POST['content'] to verify that it is valid content, and try to use

Code: Select all

mysql_real_escape_string()
on your $content before inserting into your database.

I just spotted another thing. In you INSERT statement you are writing News, and in your SELECT statement news. Be sure that it your are using caps correctly. Probably not that causing trouble, since it would come with an error.

Posted: Wed Sep 06, 2006 2:51 am
by Ollie Saunders
Confirm that there is actually stuff in the `content` field of `boarhunt.News`.

Also I just noticed that....

Code: Select all

if(isset($_POST['save']))
{ // this is probably where you want the brace
$content=$_POST['content'];


mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

// { this probably isn't.
$query = "insert into News (id,content) values ('','$content')";
mysql_query($query) or die(mysql_error('Error Query Failed'));

echo htmlspecialchars($query);
}
Other than that I can't see anything wrong.

Posted: Wed Sep 06, 2006 9:24 am
by EvoDan
OK i tried moving the brace and it wasnt entering anything into the database anymore when i moved it back im getting numbers coming out again.
Each time i submit something new it seems to add a new number so im guessing its only echoing back the row id numbers.
well it looks like that whatever comes in 'content' is false somehow, try to echo out your $_POST['content'] to verify that it is valid content, and try to use

Code: Select all

mysql_real_escape_string()
on your $content before inserting into your database.

I just spotted another thing. In you INSERT statement you are writing News, and in your SELECT statement news. Be sure that it your are using caps correctly. Probably not that causing trouble, since it would come with an error.
1. Where and how in the code should i include my echo for $_POST['content'] as im not 100% sure on the syntax and jargon yet

2. same with mysql_real_escape_string()

Thanks for your help guys i will get this eventually :lol:

Posted: Wed Sep 06, 2006 9:37 am
by Ollie Saunders
Cleaned

Code: Select all

<?php
if(!isset($_POST['content'])) {
    die('Content not specified');
}

$username = '******';
$password = '******';
$database = 'boarhunt';
$host = 'localhost';


mysql_connect($host,$username,$password) or die('Connection fail');
mysql_select_db($database) or die('Unable to select database');

$content = mysql_real_escape_string($_POST['content']);

$q = "INSERT INTO `News` (`id`, `content`) VALUES (NULL, '$content')";
mysql_query($query) or die('Error Query Failed: ' . mysql_error() . ': ' . $q);
mysql_close();
?>

Posted: Wed Sep 06, 2006 10:05 am
by EvoDan
Thanks for that m8. Now im getting content not specified so i guess its how i have the wysiwyg editor set up and it's not submitting correctly.
I will have a mess about with it and see what happens.

Posted: Wed Sep 06, 2006 11:52 am
by EvoDan
Just one other thing is there a difinative tutorial that will help me to get this set up? Either on here or another site. Because i am beginning to realise i am going to run into alot more problems like echoeing the data back into the editor form once something has been submitted and been able to edit that in some way and getting the database to change and update those records. Post your links please :D especially if you have had experience of using either FCKeditor or something similar and you know something works.