Jump to content

PHP and MySQL Form Help


Stalingrad

Recommended Posts

Hey everybody! This is probably a simple fix, but it has been a big issue for me. I have actually in the past changed the whole table to fix this. I want to be professional this time. Basically, I have created a forum. It is a basic forum. There are three separate tables for it, the posts, and topics. I want to be able to let them pick the NAME of the topic they want to create the topic in from a drop down box, but have the query insert it as an id. So, lets say, this:

 

General Chat is id 1

Help Chat is id 2

 

The drop down I select is Help chat, it will enter 2 instead of help chat. How do I do this the SIMPLE way? thanks! Here is my code fro the forums:

 

<?php
session_start();
include("config536.php");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<?php
if(!isset($_SESSION['username'])) {
echo "<banner></banner><nav>$shownavbar</nav><ubar><a href=login.php>Login</a> or <a href=register.php>Register</a></ubar><content><center><font size=6>Error!</font><br><br>You are not Logged In! Please <a href=login.php>Login</a> or <a href=register.php>Register</a> to Continue!</center></content>";
}

if(isset($_SESSION['username'])) {
echo "<nav>$shownavbar</nav><ubar>$ubear</ubar><content><center><font size=6>Boards</font><br><br>";

$getboardid = $_GET['boardid'];
$gettopicid = $_GET['topicid'];
$getpostid = $_GET['postid'];
$view = $_GET['view'];
$newtopic = $_GET['action'];

if(isset($view)) {
echo "<center>Welcoem to the Boards! Please read the <a href=tos.php>Rules</a> before posting anything. Feel free to browse whenever you'de like. <br><br><table border=1 bordercolor=black cellspacing=0 cellpadding=10><tr><td><center><b>Board Name</b></center></td><td><center><b>Description</b></center></td><td><center><b>Topics</b></center></td><td><center><b>Posts</b></center></td></tr>";
$gettq = "SELECT * FROM category WHERE catid!= '0'";
$gett = mysql_query($gettq);
while($rowy = mysql_fetch_array($gett)) {
$tid = $rowy['catid'];
$tname = $rowy['name'];
$tposts = $rowy['posts'];
$ttopics = $rowy['topics'];
$tdesc = $rowy['body'];
echo "<tr><td><center><a href=?boardid=$tid>$tname</a></center></td><td><center>$tdesc</center></td><td><center>$ttopics</center></td><td><center>$tposts</center></td></tr>";
}
echo "</table><br><br><a href=?action=create>Create a New Topic</a><br><br><br></center>";
}
if(isset($getboardid)) {
echo "<table border=1 bordercolor=black cellpadding=10 cellspacing=0><tr><td><center><b>Topic Name</b></center></td><td><center><b>Created By</b></center></td><td><center><b>Date</b></center></td></tr>";
$getbq = "SELECT * FROM topics WHERE boardin='$getboardid'";
$getb = mysql_query($getbq);
while($crow = mysql_fetch_array($getb)) {
$uname = $crow['username'];
$unm = $crow['name'];
$ubodu = $crow['body'];
$ucount = $crow['countit'];
$ucreate = $crow['created'];
$utid = $crow['topicid'];
echo "<tr><td><center><a href=?topicid=$utid>$unm</a></center></td><td><center>$uname</center></td><td>$ucreate</center></td></tr>";
}
echo "</table>";
}

if(isset($gettopicid)) {

$nowtq = "SELECT * FROM posts WHERE topicon='$gettopicid'";
$nowt = mysql_query($nowtq);
while($now = mysql_fetch_array($nowt)) {
$postidis = $now['postid'];
$poster = $now['poster'];
$thisb = $now['body'];
$getdate = $now['date'];
$gettopic = $now['topicon'];

$uq = "SELECT * FROM users WHERE username='$poster'";
$uset = mysql_query($uq);
while($row = mysql_fetch_array($uset)) {
$did = $row['userid'];
$dun = $row['username'];
$dpo = $row['posts'];
$dmp = $row['mainpet'];
$dac = $row['avatarcount'];
$dua = $row['useravatar'];
$djs = $row['jobs'];

$aq = "SELECT * FROM avatars WHERE name='$dua'";
$aaa = mysql_query($aq);
while($arow = mysql_fetch_array($aaa)) {
$theimage = $arow['image'];
}
echo "<tr><td><table border=0 cellspacing==1 cellpaddign=3 bgcolor=#F58B8E><tr><td bg color=#FFFFFF align=center valign=top width=100><font face=verdana size=3 color=#000000><br><br><a href=lookup.php?username=$dum>$dun</a><br><br><img src=/images/avatars/$theimage><br><br><br>Posts: $dpo<br><br></font></td><td bgcolor=#FFFFFF align=center valign=top width=300><font face=verdana size=3 color=black><i>Posted on: $getdate</i><br><br>$thisb</font></td></tr></table><br><br><br>";
}
}


$submitpost = $_POST['post'];
$reply = $_POST['reply'];
?>
<br><br><br><br><form action="<?php echo "$PHP_SELF"; ?>" method="POST"><center><font size=5>Post a Reply</font><br>
<textarea name="reply" rows="5" cols"32"></textarea><br><br><input type="submit" name="post" action="Post Reply"><br><br><br></form></center><?php
if(isset($submitpost)) {
if($reply == "") {
echo "<font color=red>Error! Please enter a reply!</font>";
}
if($reply != "") {
mysql_query("INSERT INTO posts (poster, body, date, topicon) VALUES ('$showusername', '$reply', '$sitetime', '$gettopicid')");
echo "<font color=green>Success! Your reply has been posted!</font>";
}
}
}

if(isset($newtopic)) {
$name = $_POST['tname'];
$tbody = $_POST['tbody'];
$tcat = $_POST['tcat'];
$nownow = $_POST['submit'];
if(!isset($nownow)) {
?>
<form action="<?php echo "$PHP_SELF"; ?>" method="POST">
Topic Title: <input type="text" name="tname"><br>
<select name="topic"><option>General Chat</option>
<option>Help Chat</option>
</select><br><br>Body:<br><textarea name="tbody" cols="38" rows="8"></textarea><br><br><input type="submit" name="submit" value="Create Topic"></form>
<?php
}
if(isset($nownow)) {
mysql_query("INSERT INTO topics (name, username, body, countit, boardin, created) VALUES ('$name', '$showusername', '$tbody', '0', '$tcat', 'sitetime')");
echo "<font color=green>Success! Your Topic has been created.</font>";
}
echo "meeba.";
}
}
?>

</html>

 

The part I'm talking about can be found down by one oft he last if statements. The one that says "if(isset($newtopic)) {"

 

I appreciate any help in advance, thank you!

Link to comment
Share on other sites

By using the value attribute for option.

// get forums
$result = mysql_query("SELECT * FROM forums");

$select = '<select name="forum">';
while ($row = mysql_fetch_assoc($result)) {
     $select .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
}
$select .= '</select>';

 

On a side note, please do some reading on how to use JOINs in SQL. Running queries in nested loops is a big no-no, especially 3 levels deep. Take a moment and think about how many queries you are potentially running. First you are looping through all the posts. Then in that same loop, you are looping through the users, then in that loop you are looping through avatars. So if you have 20 posts, you are running like 60+ queries JUST to get the forum posts. Using JOINs, you could do it like this:

SELECT p.*, u.*, a.*
FROM posts AS p
LEFT JOIN users AS u ON u.username = p.poster
LEFT JOIN avatars AS a ON a.name = u.useravatar
WHERE p.topicon='$gettopicid'

 

And then just loop through that once and you have all the same information.

 

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.