Jump to content

Am I on the right track?


Wintergreen

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? 
Link to comment
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]
Link to comment
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?
Link to comment
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... ;)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.