Another problem. Sorry

Spacemail by SpaceshipSpacemail by Spaceship
Watch

SiKing

Registered MemberEstablished Member
Impact
6
Sorry, I know I've asked a lot of questions but this SHOULD be the last.. for now. lol, just kidding.

Right, When a user submits a new thread on my forum, the thread is inserted into my "threads" table in the database and the post is submitted into the "posts" table at the same time. The thread is represented in the "threads" column of the "posts" table as a number (the id of the thread).

But the post is submitted at the same time as the thread so how will I be able to assign the post to the correct thread?

I hope that makes sense. Sorry if i wasn't very clear
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Hello,

It is kinda confusing but I kinda get it. You can have a table in Posts called tid which carries thread id.

iNod
 
0
•••
Thanks for the reply. I have a threadID column in the posts table but I'm unsure how to insert the new thread ID into it the same time it is being created. I'll try and illustrate the tables:

Posts:
ID, category, threadid, author, content, time, date

Threads:
ID, title, author, starttime, startdate

Then as you can imagine, as the user submits a new thread, I want the post inserted into the "posts" table as well as the thread into the "threads" table
 
0
•••
Hello,

First create the thread than use mysql_insert_id and set threadid and insert post.

iNod
 
0
•••
Thanks iNod. That's definately the correct method but I think I'm going wrong somewhere. it's inserting into the database the actual text "mysql_insert_id". Here's my code:

PHP:
$lastid = mysql_insert_id();

$query2 = "INSERT INTO posts VALUES ('id', '$category', $lastid, '$author', '$msg', '$starttime', '$startdate')";
$results2 = mysql_query($query2);

EDIT: Also, I get a very frustrating repetition:

http://www.darkfx.co.uk/darkfx/threads.php?category=1
 
Last edited:
0
•••
Ouch.. I suggest deleting that script from ftp right away.. Thats a major BW hog.

Now.. Lets fix it up.
First thing put ' (quotes) around $lastid

The rest seems to be correct can you post rest of php.

iNod
 
0
•••
lol. removed script for now

PHP:
if ($_POST['submitthread']) {
$msg=$_POST['msg'];
$title=$_POST['title'];
$starttime=date("H:i"); 
$startdate=date("d/m/y");





$query = "INSERT INTO forum_threads VALUES ('id', '$title', '$author', '$category', '$starttime', '$startdate')";
$results = mysql_query($query);


$lastid = mysql_insert_id();

$query2 = "INSERT INTO forum VALUES ('id', '$category', '$lastid', '$author', '$msg', '$starttime', '$startdate')";
$results2 = mysql_query($query2);

if (!$result) {
include("head.php");
echo("<h1>Success</h1> <p>Post added successfully. <a href='thread.php?category=$category&thread=$lastid'>Return to forum</a></p>
");
include("foot.php");
exit;
}}
 
0
•••
PHP:
if($_POST['submitthread']) {
$msg=$_POST['msg'];
$title=$_POST['title'];
$starttime=date("H:i");
$startdate=date("d/m/y");





$query = "INSERT INTO forum_threads VALUES ('$title', '$author', '$category', '$starttime', '$startdate')";
$results = mysql_query($query);


$lastid = mysql_insert_id();

$query2 = "INSERT INTO forum VALUES ('$category', '$lastid', '$author', '$msg', '$starttime', '$startdate')";
$results2 = mysql_query($query2);

if (!$result) {
include("head.php");
echo("<h1>Success</h1> <p>Post added successfully. <a href='thread.php?category=$category&thread=$lastid'>Return to forum</a></p>
");
include("foot.php");
exit;
}
}

Is there any code about that like while or foreach?

I am wondering why it is looping.

iNod
 
0
•••
Well, the page which displays the threads is:

PHP:
<?
include("config.php");
$category = $_GET['category'];
$newthread = $_GET['newthread'];
session_start();
$author = $_SESSION['username'];

// DISPLAY THREADS
if (isset($_GET['category'])) {


$result = mysql_query("SELECT * FROM categories WHERE id='$category'") 
or die("SELECT Error1: " .mysql_error());
$num=mysql_numrows($result);
while ($row = mysql_fetch_array($result)) 
{ 
$title = $row['title'];
}
$pagetitle = "$title threads";
include("head.php");

echo "<h1>$title forum</h1>
<table width='100%' border='0'>
<tr><td align='left'><p><a href='forum.php'>< Back to categories</a></p></td>
<td align='right'><p><a href='forumhandle.php?newthread=true&category=$category'>Start new thread</a></p></td></tr></table>";
echo "<table width='100%' border='0' cellspacing=0 cellpadding=2>
<tr class='threadhead'>
	<td width='40%'>Thread</td>
	<td width='25%'>Thread Started</td>
	<td width='25%'>Last Post</td>
	<td width='10%'>Posts</td>
</tr>";
// END OF HEADINGS





$result = mysql_query("SELECT * FROM forum_threads WHERE category='$category' ORDER BY id DESC") 
or die("SELECT Error1: " .mysql_error());
$num=mysql_numrows($result);


$i=0;
while ($i < $num) {
if ($i % 2) {
$bgcolor = "threadtable";
			}
else {
$bgcolor = "threadtable2";
	 }



$id=mysql_result($result,$i,"id");
$title=mysql_result($result,$i,"title");
$author=mysql_result($result,$i,"author");
$category=mysql_result($result,$i,"category");
$startdate=mysql_result($result,$i,"startdate");
$starttime=mysql_result($result,$i,"starttime");


$query = mysql_query( "SELECT * FROM forum WHERE threadid='$id' AND category='$category'" ) or die("SELECT Error2: " .mysql_error());
$numero=mysql_numrows($query);
$query = mysql_query( "SELECT id FROM users WHERE username='$author'" ) or die("SELECT Error3: " .mysql_error());
$authorid=mysql_result($query,0,"id");


$query = mysql_query( "SELECT * FROM forum WHERE threadid='$id' AND category='$category' ORDER BY 'datetime' ASC LIMIT 1" ) or die("SELECT Error4: " .mysql_error());
$num1=mysql_numrows($query);
$i=0;
while ($i < $num1) {
$author=mysql_result($query,$i,"author");
$time=mysql_result($query,$i,"time");
$date=mysql_result($query,$i,"date");
$today = date("d/m/y");

if ($date == $today) {$date = "Today"; }
{$date = "Today";}
$i++;
}







echo "
  <tr class='$bgcolor'>
    <td valign='top'><a href='showthread.php?category=$category&thread=$id'>$title</a></td>
    <td valign='top'>$startdate at $starttime <br />by <a href='viewprofile?profile_id=$authorid'>$author</a></td>
    <td valign='top'>$date at $time</td>
	<td valign='top'>$numero</td>
  </tr>";
	 
$i++;
}
echo "</table>";
include("foot.php");
}
// END DISPLAY THREADS

?>

There are a few loops. But I think it's because of the database content which was the reason it was repeating
 
0
•••
I got the ThreadID thing orking. But the problem now is that when displaying the threads, it isn't looping. It only displays the last row in the database
 
0
•••
Hello,

Get read of that while $i stuff. Use this format

PHP:
$query = mysql_query("SELECT * FROM bob");

while($row = mysql_fetch_array($query)) {
print "Bob eats $row['candy']";
}

Above says.
Select all from bob. (Table I made was candy)
While $row is equal to mysql_fetch_array do.
print "Bob eats candy or what not";
}

iNod
 
0
•••
Thank you so much once again! NP$ donated!!
 
0
•••
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back