Member Avatar for champmanking

Hello all,

I am looking for help on how to code this:

I would like to have a page where there is a box where registered users can type something then on pressing the button a new page is created displaying the text they entered and boxes for other people to type into.

Thanks in advance.

Member Avatar for diafol

Are you looking for a commenting system?

Please post some code so that users can help guide you. Don't expect anyone here to do your job for you =)

Member Avatar for champmanking

Yeah, I suppose a comment system where a page is created with information already entered and boxes for people to comment/post in.

I want the new page to last forever, or until it is deleted from the database. It'd be nice if the url ended with an id number so the pages can be found.

I don't have any code to show before I don't know where to start or even what to do. I'm not asking for the full system, just code snippets or whatever.

Member Avatar for champmanking

I've attempted it but my code doesn't seem to work - throws back HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request. Can anyone take a look? Sorry for double posting.

<html>
<head>
</head>
<body>
<?php
if (isset($_POST['mytext']) { 

$myt = $_POST['mytext'];

$mymysql = mysql_connect("host","user","pass") or die ("can't connect");

mysql_select_db("dbname", $mymysql );
$sql = "INSERT INTO content (content) VALUES ('$myt')";
mysql_query( $sql, $mymysql );

$sql = "SELECT * FROM content WHERE content = '$myt'";
$row = mysql_query( $sql, $mymysql );

$myid=$row['id'];
mysql_close ($mymysql);



?>
Your text has been added. Your link:<br>
<a href="http://website.com/link.php?id=<?php print ($myid) ?>"> Link</a>

<?php } else { ?>

<form action="blah.php" method="POST">
<input type="text" value="type your text here" name="mytext">
<input type="submit">
</form> 
<?php } ?>
</body>
</html>

blah.php = itself

I've coded link.php which seems to work.

Member Avatar for diafol

Set up the DB - for a simple system with tags for posts (pages):

posts (pages)
post_id (PK)
post_title
post_body
user_id (FK)
post_timestamp

tags
tag_id (PK)
tags_string

post_tags (link table)
post_id (FK)
tag_id (FK)

comments
comment_id (PK)
user_id (FK)
post_id (FK)
comment_timestamp

You have just one page for displaying posts, lets call it posts.php
You then access the relevant post record with an 'id' parameter in the querystring:

<a href="posts.php?id=7">How to create a DB in mysql</a>

Those are the bare bones of the system IMO.

Member Avatar for champmanking

I'm having trouble creating the custom link and displaying the information. Can anyone provide some code for a basic version?

Member Avatar for diafol

Can anyone provide some code for a basic version?

That's what Google is for. If you're stuck on a particular part of the code, paste what you have and we'll help. The purpose of this forum is not to be a 'rent-a-sap' site, rather to help users with their own code. If it's a trivial thing, well fair enough, but a full commenting system is not a trivial thing.

I suppose a pro could knock one up in about 20 minutes or less, but again that's not the purpose of this forum.

Member Avatar for champmanking

I posted my code.

Member Avatar for diafol

I posted my code.

Yes you did, but you also asked for somebody to provide you with a basic system.

RE: your code, use mysql_insert_id() to get the last added id - no need to do a SELECT.

Also you don't clean your input, so any single quotes in the $myt will kill the query.

Member Avatar for champmanking

It still doesn't seem to show new link, create a new page and therefore show the text. I posted my code below.

example.php

<?php

if (isset($_POST['mytext'])) { 


$myt = $_POST['mytext'];

$mymysql = mysql_connect("","","") or die ("can't connect");

mysql_select_db("", $mymysql );
$sql = "INSERT INTO content (content) VALUES ('$myt')";
mysql_query( $sql, $mymysql );

$sql = mysql_insert_id();
$row = mysql_query($sql, $mymysql);

$myid=$row['id'];
mysql_close ($mymysql);


echo "<a href='http://site.com/link.php?id='.$myid>Link</a>";
}
?>

link.php

<?php


if (isset($_GET['id'])) {
$myid = $_GET['id'];
$mymysql = mysql_connect("","","") or die ("can't connect");

mysql_select_db("", $mymysql );
$sql = "SELECT * FROM content WHERE id = '$myid'";
$row = mysql_query( $sql, $mymysql );

if ($row) {
print ($row['content']);
             } 
mysql_close ($mymysql);
           

                         }
?>
Member Avatar for champmanking

Bump :o

Member Avatar for diafol
mysql_select_db("", $mymysql );
$sql = "INSERT INTO content (content) VALUES ('$myt')";
mysql_query( $sql, $mymysql );

$id = mysql_insert_id();
$myid=$id;
mysql_close ($mymysql);
Member Avatar for champmanking

Thanks, now it creates new url. However, the text doesn't show on the page. How would I go about showing it?

Member Avatar for diafol

So you get the link: http://site.com/link.php?id=7 or similar.

Your code in the link.php is buggered. Is that right?

$sql = "SELECT * FROM content WHERE id = '$myid' LIMIT 1";
$row = mysql_query( $sql, $mymysql );
if (mysql_num_rows($row)>0) {
  $d = mysql_fetch_array($row);
  echo "Content exists:<br />";
  echo $d['content']);
}else{
  echo 'No content for this ID';
}
Member Avatar for champmanking

Just tried that code for link.php and it displays an error.

<?php


if (isset($_GET['id'])) {
$myid = $_GET['id'];
$mymysql = mysql_connect("","","") or die ("can't connect");

mysql_select_db("", $mymysql );
$sql = "SELECT * FROM content WHERE id = '$myid' LIMIT 1";
$row = mysql_query( $sql, $mymysql );
if (mysql_num_rows($row)>0) {
  $d = mysql_fetch_array($row);
  echo "Content exists:<br />";
  echo $d['content']);
}else{
  echo 'No content for this ID';
}
}
?>
Member Avatar for diafol

Are you going to share the error message or am I supposed to guess...:)

Although it could be this:

echo $d['content']);

change to this:

echo $d['content'];
Member Avatar for champmanking

Solved! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.