Jump to content

Archived

This topic is now archived and is closed to further replies.

Wintergreen

Am I on the right track?

Recommended Posts

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.
[code]
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)
)
[/code]

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
[CODE]
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());
[/CODE]

And then to list the stuff, I'd do:
[CODE]
$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 />';
}
[/CODE]

Would this utterly simplistic way of doing things work? 

Share this post


Link to post
Share on other sites
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:

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

Share this post


Link to post
Share on other sites
Okay, I had a problem arise (surprise surprise).
http://www.potentiates.com/pandafactory/meganform.html

The source of the page is here :
[code]
<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>
[/code]

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. 
[code]
<?
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;
*/

?>
[/code]

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

Share this post


Link to post
Share on other sites
The first problem I can see is that the value from these radio buttons...

[code]
<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 />
[/code]

...are not being assigned to a variable in your [b]makeentry.php[/b] file.

You have:

[code=php:0]
$posttype = $_POST['post_type'];
[/code]

but with the name of the input field being [b]posttype[/b], that should read:

[code=php:0]
$posttype = $_POST['posttype'];
[/code]


Next, this conditional will never be executed...

[code=php:0]
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();
}
[/code]

... 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):

[code=php:0]
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();
             }
}
[/code]

Your query looks correctly formatted, but if you could post the contents of your [b]db.php[/b] 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... ;)

Share this post


Link to post
Share on other sites
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!).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.