Jump to content

Text storage and database selection


CynePhoba

Recommended Posts

Hey guys,

 

I'm trying to build a simple 'blogging engine' as a fun side project. At the moment i'm mainly having trouble with 2 things -

 

1. What is the best way that the administrator can input the data (with linebreaks, etc) and then have it stored in the database with this formatting so that when it is called from the database, it shows up the same on the page?

 

2. How do i go about selecting only certain posts from the database? I have tried doing a /post.php?id=12 and then an SQL statement "WHERE post_id=" . $id (in the correct formatting, of course) but this didnt seem to work.

 

 

Any help would be appreciated.

 

-CynePhoba

Link to comment
Share on other sites

1. If an admin is doing all the posts, just use TinyMCE or similar to allow them to create HTML in the browser and insert that HTML directly into the table.  If a 3rd party enters text, it's best to strip_tags and replace breaks with \n or paragraphs with \n\n going into the table, then replace \n with <br /> for display.

 

2. Try using "... WHERE post_id = " . ((int) $_GET['id']);

Link to comment
Share on other sites

1. You don't need to do anything to store linebreaks. If you are submitting data from a textarea and there are linebreaks in the textarea, there will be linebreaks in the database. They are invisible characters so you cannot see them, but they are there. In order to make them appear on the page once you retrieve the information all you need to do is use nl2br to convert the invisible \n characters to HTML <br /> tags.

 

2. But what is $id?

Link to comment
Share on other sites


include '/static/includes/dbconnect.php';

if (isset($_post['pid']))
{
$pid=$_get['pid'];
$postquery = "SELECT * FROM posts WHERE postid =" . $pid;
$resultposts = mysql_query ($postquery);

if (!$resultposts) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $resultposts;
    die($message);
}

while ($row = mysql_fetch_assoc($resultposts)) {
    echo $row['posttitle'];
echo '<br/>';
    echo $row['postcontent'];
echo '<br/>';
echo '<br/>';
}

}

 

That's what I'm using to read $id. It is sent via the URL.

Link to comment
Share on other sites

$_POST is for forms, you'll need to use $_GET.

Also, pick id or pid, don't have the url say id and then expect to access it with pid

 

Lastly,

"$message .= 'Whole query: ' . $resultposts;

 

Will not show you your query.

Link to comment
Share on other sites

Yes, I will do that.

 

Also one quick question - at the moment I'm posting the text from the text box via URL to the next page, where it is inserted into the text box. I haven't done a lot of php, but I get the feeling that this is the wrong way to be doing this.

 

How should I go about getting the text from the text box and putting it into the database?

Link to comment
Share on other sites

<form method="post">

 

Always use $_POST data for forms.  For one thing, the querystring is limited in the amount of data it can handle.  Post also prevents a lot of simple hacks to your site.

 

Besides that, there's the whole HTTP REST spec.

 

GET should only be used for retrieval

POST for saving data

 

We won't even get into PUT and DELETE... save that lesson for later!

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.