query problem?

Hello again :slight_smile: could use some help with this…

At the moment I have a post system on my index page which displays the latest posts made by any administrator. The administrators can of course log in to the site at any time and then choose to make a new post.

The problem right now is that the post doesn’t say who submitted the post, when in fact it should display the administrator’s name.

I have two tables here, one that stores the post’s information such as title, body, id, date and poster. And then I have another table that contains information about the administrator like first name and username etc.

Right now it returns “Resource id #10” as $poster instead of the first name

[php] if($_POST[‘post’]) {
$title = $_POST[‘title’];
$body = $_POST[‘body’];

	$title = htmlentities($title, ENT_COMPAT, 'ISO-8859-15');
	$body = htmlentities($body, ENT_COMPAT, 'ISO-8859-15');
	
	if($title && $body) {
                    // get current date
		$date = date("Y-m-d");
		
                    // get user id
		$userid = $_SESSION['user_id'];
		
                    // get poster
		$poster = mysql_query("SELECT `first_name` FROM `users` WHERE `user_id` = '$userid'") or die(mysql_error());
		
		// insert the information into table				
		$insert = mysql_query("INSERT INTO `news` VALUES ('$id', '$title', '$body', '$date', '$poster')") or die(mysql_error());

                    // redirect
	 	header('Location: post.php?success');
		exit();
	} else {
		$errors[] = 'Both fields are required.';
	}
}[/php]

Any help is appreciated…

there is nothing wrong with your code.

however you missed something
[php]
$row = mysql_fetch_assoc($poster) or die(MYsql_error());

		//you can use the name whereever you want like this
		$row['first_name'];

[/php]

But I already have that in the index page, this grabs the information from the news table. Whereas in the other file it inserts the information to the news table.

[php]<?php
$getnews = mysql_query(“SELECT * FROM news ORDER BY id DESC”) or die(mysql_error());

while ($row = mysql_fetch_assoc($getnews)) {
	$id = $row['id'];
	$title = $row['title'];
	$body = $row['body'];
	$date = $row['date'];				
	$poster = $row['poster'];

?>[/php]

nevermind, fixed it…

sorry i looked at your code yesterday and i didnt see anything wrong.

what was the issue anyway?

I forgot to use the mysql_fetch_assoc() function on the $query :stuck_out_tongue:

I think that’s what i posted on my first reply XD

yes but I changed the content a bit :stuck_out_tongue_winking_eye:

Ahh hah XD

Good Luck Buddy see you around

Happy Programming

Sponsor our Newsletter | Privacy Policy | Terms of Service