Jump to content


Photo

Am I on the right track?


  • Please log in to reply
6 replies to this topic

#1 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 01 September 2006 - 02:33 PM

I'm trying to create a simple blog, and I do mean simple.  I'm just posting this to make sure I'm on the right track.  This is somewhat overwhelming for me, as I'm not an amazing coder by any means and can get incredibly frustrated doing something even this simple.  At any rate, this is obviously the most basic interaction possible between PHP and MySQL, but it anyone's willing to confirm that I'm on the right track or guide me towards it, I'd pee myself in joy

This is my table setup for the posts.
CREATE TABLE posts (
  postid int(25) NOT NULL auto_increment,
  title varchar(30) NOT NULL default '',
  post_body varchar(6000) NOT NULL default'',
  post_time datetime NOT NULL default '0000-00-00 00:00:00',
  post_type enum('0','1','2','3') NOT NULL default '0',
  PRIMARY KEY (postid)
)

The post types would be like, general blog update, art posts, etc.  Just a way to break them up by type. 

As far as inserting into the DB goes, I was looking at using something along the lines of
mysql_query("INSTERT INTO posts (title, post_body, post_time, post_type) VALUES('$title_text','$body_text','$date_time','$post_type')") or die(mysql_error());

And then to list the stuff, I'd do:
$sql = "SELECT title, post_body FROM posts WHERE post_type = x"; //whichever posttype I want
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
    echo $row['name'].'<br />';
    echo $row['email'].'<br />';
}

Would this utterly simplistic way of doing things work? 

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 September 2006 - 03:20 PM

Close.  I'd have used a type of text for the blog post.

Your real problem's with the retrieval query.  If all you get is title and post_body then you're nto going to find name and email in the return row. Come to that, how would you get name and email when they're not even in the database?  Perhaps you really wanted:

echo $row['title']. '<br/>'. nl2br($row['post_body']);

Legend has it that reading the manual never killed anyone.
My site

#3 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 01 September 2006 - 05:15 PM

Okay, I had a problem arise (surprise surprise).
http://www.potentiat.../meganform.html

The source of the page is here :
<html>
<body>

<form name="entry" method="post" action="makeentry.php">
	<table width="600" cellspacing="0" cellpadding="0">
	<tr><td><input type="radio" name="posttype" value="0"> General Update<br />
		<input type="radio" name="posttype" value="1"> Picture Update<br />
		<input type="radio" name="posttype" value="2"> Art Update<br /></td></tr>
	<tr><td>Entry Title</td></tr>
	<tr><td width="100%"><input name="posttitle" type="text" size="45"></td></tr>
	<tr><td>Body</td></tr>
	<tr><td width="100%"><textarea name="bodytext" type="text" rows="20" cols="60"></textarea></td></tr>
	<tr><td><input name="Submit" type="submit" value="Submit"></td></tr>
	</table>
</form>

</body>
</html>

I have to be doing something wrong with the form and the makeentry.php.  I editted out all the stuff beyond making the variables, and yet in FireFox I get a string of Chinese Characters and in IE it shows the title, as I would expect.

This is what I did for the makeentry.php page. 
<?
	include 'db.php';

//Make data into vars
	$posttype = $_POST['post_type'];
	$posttitle = $_POST['posttitle'];
	$post_body = $_POST['bodytext'];

	echo "$posttitle";
/*
if((!$posttype) || (!$posttitle) || (!$post_body)) {
	if(!$posttype){
		$post_type = 0;
	}
	if(!$posttitle){
		$posttitle = "None";
	}
	if(!$post_body) {
		echo "You need to make an update, duh!";
	include "update.php";
	exit();
}

$post_time = date("Y-m-d H:i:s");

//Enter info into the db
mysql_query("INSERT INTO posts (title, post_body, post_time, post_type) VALUES('$posttitle','$post_body','$post_time','$posttype')") or die(mysql_error());

header("index.php");
exit;
*/

?>

Is there something wrong with my form?  Or in the variable at the top?

#4 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 01 September 2006 - 06:11 PM

The first problem I can see is that the value from these radio buttons...

<input type="radio" name="posttype" value="0"> General Update<br />
<input type="radio" name="posttype" value="1"> Picture Update<br />
<input type="radio" name="posttype" value="2"> Art Update<br />

...are not being assigned to a variable in your makeentry.php file.

You have:

$posttype = $_POST['post_type'];

but with the name of the input field being posttype, that should read:

$posttype = $_POST['posttype'];


Next, this conditional will never be executed...

if((!$posttype) || (!$posttitle) || (!$post_body)) {
	if(!$posttype){
		$post_type = 0;
	}
	if(!$posttitle){
		$posttitle = "None";
	}
	if(!$post_body) {
		echo "You need to make an update, duh!";
	include "update.php";
	exit();
}

... because right before it you defined all three of the variables you check the existance of. The variables may not contain any information, but they do exist. Try changing your conditional to (some syntax fixes included as well):

if( empty($posttype) || empty($posttitle) || empty($post_body) ){
	if( empty($posttype) ){
		$posttype = 0;
	}
	if( empty($posttitle) ){
		$posttitle = "None";
	}
	if( empty($post_body) ){
		echo "You need to make an update, duh!";
	include "update.php";
	exit();
             }
}

Your query looks correctly formatted, but if you could post the contents of your db.php file, we can ensure that you're calling the DB connection properly.

If you are getting any errors when loading the page, please post them here as well.

Good luck... ;)

#5 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 02 September 2006 - 04:13 AM

Thanks for the reply.  The Chinese stuff popped up because I had switched encoding to Unicode, and disappeared after I switched it back.    Everything now connects and works properly (fixed the empty() stuff too, thanks!).


#6 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 02 September 2006 - 04:44 AM

Seems like every time I finally post I figure it out 2 minutes later. 

When I originally passed the variable I had done  <a href=edit.php?" . $var instead of edit.php?id=" . $var
So I was getting sent to edit.php?32 instead of edit.php?id=32

Such small errors :P

#7 rallokkcaz

rallokkcaz
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts
  • LocationSomewhere, CA (Cactus Area)

Posted 02 September 2006 - 04:48 AM

can we see the rest of the code?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users