Utilizing multiple db tables in one code..

Jadenrose

Weaksauce
Joined
Apr 30, 2005
Messages
102
Ok, I've run into another dilemma that I have been trying to recover on my own. What I want to do is this.

I have a form that people use to submit information with. What I want the code to do is, once the form has been submitted, use the chosen category to get the id for that category from one table, and input it into another table.

Here's where I currently stand:

Code:
<?php #add_topic.php
//This is the add topic page for MX Employees.

//Include the configuration file for error and management and such.
require_once ('includes/config.inc.php');

//Set the page title and include the HTML header.
$page_title = 'Add a Topic';
include ('includes/employheader.html');

//Set time zone.
date_default_timezone_set("CST6CDT");

//Open the database.
include ('employ_mysql_connect.php');

if (isset($_POST['submitted'])) { //Handle the form.

	require_once ('employ_mysql_connect.php');
	//Connect to the database.
	
	//Check for a topic name.
	if (!empty($_POST['topic_name'])) {
		$tn = escape_data($_POST['topic_name']);
	} else {
		echo '<p><font color="red">You forgot to enter your topic\'s name.</font></p>';
	}
	
	//Check for a topic date.
	if (!empty($_POST['topic_date'])) {
		$td = escape_data($_POST['topic_date']);
	} else {
		echo '<p><font color="red">You forgot to enter your topic\'s date.</font></p>';
	}
	
	//Check for a user.
	if (!empty($_POST['submitted_by'])) {
		$sb = escape_data($_POST['submitted_by']);
	} else {
		echo '<p><font color="red">You forgot to enter your username.</font></p>';
	}
	
	//Check for question.
	if (!empty($_POST['question'])) {
		$q = escape_data($_POST['question']);
	} else {
		echo '<p><font color="red">You forgot to enter your question.</font></p>';
	}
	
	//Check for an answer.
	if (!empty($_POST['answer'])) {
		$a = escape_data($_POST['answer']);
	} else {
		echo '<p><font color="red">You forgot to enter your answer.</font></p>';
	}
	
	//Check for a category.
	if (!empty($_POST['cat_name'])) {
		$c = escape_data($_POST['cat_name']);
	} else {
		echo '<p><font color="red">You forgot to enter your category.</font></p>';
	}
	
	if ($tn && $td && $sb && $q && $a && $c) { //If everythings ok.
	
		//First get the cat_id from categories.
		$query = "SELECT cat_id FROM kb_categories WHERE cat_name=cat_name";
		$result = mysql_query ($query) or trigger_error("Query: $query: $query\n<br />MySQL Error: " . mysql_error());
		
		if (mysql_num_rows($result) == 0) {//Not there
			echo '<p><font color="red" size="+1">Cat Id Unavailable.</font></p>';
		} else {
			$cid = escape_data($_GET['cat_id']);
		}
		
		//Make sure the topic name is available.
		$query = "SELECT topic_name FROM kb_topics WHERE topic_name='$tn'";
		$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
		
		if (mysql_num_rows($result) == 0) { //Available.
			
			//Add the topic.
			$query = "INSERT INTO kb_topics (topic_name, topic_date, submitted_by, question, answer, cat_name, cat_id) VALUES ('$tn', '$td', '$sb', '$q', '$a', '$c', '$cid')";
			$result = mysql_query ($query) or trigger_error("Query: $query: $query\n<br />MySQL Error: " . mysql_error());
			
			if (mysql_affected_rows() == 1) { //If it ran ok.
				
				//Finish the page.
				echo '<h3>New topic added!</h3>';
				include ('includes/employfooter.html'); //Include the HTML footer.
				exit();
				
			} else { //If it did not run ok.
				echo '<p><font color="red" size="+1">The topic could not be added due to a system error.  Sorry for the inconvenience.</font></p>';
			}
			
		} else { // The topic name is not available.
			echo '<p><font color="red" size="+1">That topic name has already been used.</font></p>';
		}
		
	} else { //If one of the data tests failed.
		echo '<p><font color="red" size="+1">Please try again.</font></p>';
	}
	
} //End of the main Submit conditional.
?>

<h1>Add Topic:</h1>
<form action="add_topic.php" method="post">
	<fieldset>
	
	<p><b>Topic Name:</b> <input type="text" name="topic_name" size="15" maxlength="15" /></p>
	
	<p><b>Topic Date:</b> <input type="text" name="topic_date" size="20" maxlength="20" value="<?php echo date("Y-m-d"); ?>" readonly></p>
	
	<p><b>Submitted By:</b> <input type="text" name="submitted_by" size="35" maxlength="40" value="<?php echo $_SESSION['username']; ?>" readonly></p>
	
	<p><b>Question:</b> <input type="text" name="question" size="65" maxlength="65" /></p>
	
	<p><b>Answer:</b> <textarea name="answer" rows="3" cols="65" wrap="wrap"></textarea>
	
	<p><b>Category:</b> <select name="cat_name"><option>Select One</option>
	<?php
	$query = "SELECT * FROM kb_categories";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
		echo "<option value=\"$row[1]\" selected=\"yes\">$row[1]</option>
		";
	}
	?>
	
	</select></p>
	
	<p><input type="hidden" name="submitted" value="TRUE" />
	<div align="center"><input type="submit" name="submit" value="Submit Topic" /></p></div>
	</fieldset>
</form>

<?php //Include the HTML footer.

mysql_close(); //Close the database connection.

include ('includes/employfooter.html');
?>

Any help is as always appreciative. Any extra info need is available upon request. Thanks!
 
Its not calling the id from the other table and inputing it in the topic table.
 
this set of lines here is confusing me:
Code:
$query = "SELECT cat_id FROM kb_categories WHERE cat_name=cat_name";
		$result = mysql_query ($query) or trigger_error("Query: $query: $query\n<br />MySQL Error: " . mysql_error());
		
		if (mysql_num_rows($result) == 0) {//Not there
			echo '<p><font color="red" size="+1">Cat Id Unavailable.</font></p>';
		} else {
			$cid = escape_data($_GET['cat_id']);
		}

First, your query looks jacked. WHERE cat_name=cat_name is always matching itself, for one...
However, you have earlier $c=$_POST['cat_name']

Do you mean to do WHERE cat_name=$c?


Also, you have $cid = escape_data($_GET['cat_id']);
Are you passing the cat_id via the URL? (because that's what the GET array contains...)

If you ARE, then why are you querying the Db at all for a category?
Are you just checking to see if it exists / is a proper category?

Your INSERT query is trying to insert the value of $cid into the database... but $cid doesn't get set UNLESS the query returns results AND you are passing it in via the URL
 
Well that part of the code, I had been trying all sorts of ways to get the cat_id from the categories table and input it into the topics table. I figured it was probably wrong. The where clause was supposed to mean, get the cat_id when the cat_name(gotten from the form when its submitted) matches. The categories are already inputed on the categories table and assigned an id when its entered. When someone is submitting a question, they choose the category that it needs to be placed in. I want the script to take that category name chosen and get the id for it.
 
Ok, in that case you're probably better off eliminating that entire query altogether.

Because you build your drop-down selection, using the categories that the person has to choose from, and submit them with the form.

seen here:
Code:
	$query = "SELECT * FROM kb_categories";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
		echo "<option value=\"$row[1]\" selected=\"yes\">$row[1]</option>
		";
	}

(I'm assuming that the kb_categories table just has cat_id and cat_name columns)

Instead of using the "name" ($row[1]) as the value for the option, why not just use the cat_id? ($row[0] in this case)

This way, when the form is submitted, you have already passed the proper cat_id.
This way, you eliminate a whole set of code and a query.

There is also another issue with that section of code, you are setting EVERY option to selected... which you can't do unless it's a multi-selection
 
I'll try it with the row 0 and see if that works.

I thought I'd taken out the mutliple selecting value..How do I have it set up for multiple selection?

edit:
and yeah, the categories table only has cat_id and cat_name.
 
I'll try it with the row 0 and see if that works.

I thought I'd taken out the mutliple selecting value..How do I have it set up for multiple selection?

edit:
and yeah, the categories table only has cat_id and cat_name.

Just changing it to row[0] isn't going to work, you'd need to adjust the rest of the code.

when you are building the selection, you have every <option value="" with a selected="yes" in them.

just remove the selected="yes" and you should be ok
 
Ok, I get what your saying. The cat_name is the name of the selected field...so that's where it gets the cat_name, but without the cat_id being in a named field, how would I retrieve that information once its passed.

stupid cold is putting pressure on my head and making me feel totally dumb.
 
Ok, I get what your saying. The cat_name is the name of the selected field...so that's where it gets the cat_name, but without the cat_id being in a named field, how would I retrieve that information once its passed.

stupid cold is putting pressure on my head and making me feel totally dumb.



well, that's a bigger issue...

From what I see in your topics table, you need both the cat_name and cat_id.

What I would have done, is just required the cat_id, and then queried the Db for the name if I needed it.


I guess to work around this, when you build the selection you can kind of fudge it.

build the <option value kind like this:
value = $row[0] . "|" . $row[1]

then later in the code, you can use split on the passed string to split out the cat_id and cat_name again.

hint: $cat_info = split("|", $_POST['cat_name'])
$cat_info[0] should be the id, and $cat_info[1] should be the name.
 
I'm trying to make it work correctly..but not having used split before, I'm looking for some information on it to know how it works and where it should go and such..
 
Yeah, I found that and tried to follow it by example and am now getting the following error:

Fatal error: Can't use function return value in write context in /home/u3/motorx1/html/kb/employee/add_topic.php on line 74

Here's where I stand with the changes in the coding:

Code 1:
Code:
//Split the category information.
		$list($cid, $cn) = split("|", $_POST['cat_name']);

and the actually selection menu:
Code:
<p><b>Category:</b> <select name="cat_name"><option>Select One</option>
	<?php
	$query = "SELECT * FROM kb_categories";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
		echo "<option value=\"$row[0] . "|" . $row[1]\">$row[1]</option>
		";
	}
	?>
	
	</select></p>
 
Yeah, I found that and tried to follow it by example and am now getting the following error:



Here's where I stand with the changes in the coding:

Code 1:
Code:
//Split the category information.
		$list($cid, $cn) = split("|", $_POST['cat_name']);

and the actually selection menu:
Code:
<p><b>Category:</b> <select name="cat_name"><option>Select One</option>
	<?php
	$query = "SELECT * FROM kb_categories";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
		echo "<option value=\"$row[0] . "|" . $row[1]\">$row[1]</option>
		";
	}
	?>
	
	</select></p>

in the "echo" portion, your quotes are off...

it should be: echo "<option value=\"" . $row[0] . "|" . $row[1] . "\">$row[1]</option>";
 
Code:
<?php #add_topic.php
//This is the add topic page for MX Employees.

//Include the configuration file for error and management and such.
require_once ('includes/config.inc.php');

//Set the page title and include the HTML header.
$page_title = 'Add a Topic';
include ('includes/employheader.html');

//Set time zone.
date_default_timezone_set("CST6CDT");

//Open the database.
include ('employ_mysql_connect.php');

if (isset($_POST['submitted'])) { //Handle the form.

	require_once ('employ_mysql_connect.php');
	//Connect to the database.
	
	//Check for a topic name.
	if (!empty($_POST['topic_name'])) {
		$tn = escape_data($_POST['topic_name']);
	} else {
		echo '<p><font color="red">You forgot to enter your topic\'s name.</font></p>';
	}
	
	//Check for a topic date.
	if (!empty($_POST['topic_date'])) {
		$td = escape_data($_POST['topic_date']);
	} else {
		echo '<p><font color="red">You forgot to enter your topic\'s date.</font></p>';
	}
	
	//Check for a user.
	if (!empty($_POST['submitted_by'])) {
		$sb = escape_data($_POST['submitted_by']);
	} else {
		echo '<p><font color="red">You forgot to enter your username.</font></p>';
	}
	
	//Check for question.
	if (!empty($_POST['question'])) {
		$q = escape_data($_POST['question']);
	} else {
		echo '<p><font color="red">You forgot to enter your question.</font></p>';
	}
	
	//Check for an answer.
	if (!empty($_POST['answer'])) {
		$a = escape_data($_POST['answer']);
	} else {
		echo '<p><font color="red">You forgot to enter your answer.</font></p>';
	}
	
	//Check for a category.
	if (!empty($_POST['cat_name'])) {
		$c = escape_data($_POST['cat_name']);
	} else {
		echo '<p><font color="red">You forgot to enter your category.</font></p>';
	}
	
	//Check for keywords.
	if (!empty($_POST['keywords'])) {
		$k = escape_data($_POST['keywords']);
	} else {
		echo '<p><font color="red">You forgot to specify keywords and/or phrases.</font></p>';
	}
	
	if ($tn && $td && $sb && $q && $a && $c && $k) { //If everythings ok.
	
		//Split the category information.
		$list($cid, $cn) = split("|", $_POST['cat_name']);
	
		//Make sure the topic name is available.
		$query = "SELECT topic_name FROM kb_topics WHERE topic_name='$tn'";
		$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
		
		if (mysql_num_rows($result) == 0) { //Available.
			
			//Add the topic.
			$query = "INSERT INTO kb_topics (topic_name, topic_date, submitted_by, question, answer, cat_name, keywords, cat_id) VALUES ('$tn', '$td', '$sb', '$q', '$a', '$cn', '$k', '$cid')";
			$result = mysql_query ($query) or trigger_error("Query: $query: $query\n<br />MySQL Error: " . mysql_error());
			
			if (mysql_affected_rows() == 1) { //If it ran ok.
				
				//Finish the page.
				echo '<h3>New topic added!</h3>';
				include ('includes/employfooter.html'); //Include the HTML footer.
				exit();
				
			} else { //If it did not run ok.
				echo '<p><font color="red" size="+1">The topic could not be added due to a system error.  Sorry for the inconvenience.</font></p>';
			}
			
		} else { // The topic name is not available.
			echo '<p><font color="red" size="+1">That topic name has already been used.</font></p>';
		}
		
	} else { //If one of the data tests failed.
		echo '<p><font color="red" size="+1">Please try again.</font></p>';
	}
	
} //End of the main Submit conditional.
?>

<h1>Add Topic:</h1>
<form action="add_topic.php" method="post">
	<fieldset>
	
	<p><b>Topic Name:</b> <input type="text" name="topic_name" size="30" maxlength="30" /></p>
	
	<p><b>Topic Date:</b> <input type="text" name="topic_date" size="20" maxlength="20" value="<?php echo date("Y-m-d"); ?>" readonly></p>
	
	<p><b>Submitted By:</b> <input type="text" name="submitted_by" size="35" maxlength="40" value="<?php echo $_SESSION['username']; ?>" readonly></p>
	
	<p><b>Question:</b> <input type="text" name="question" size="65" maxlength="65" /></p>
	
	<p><b>Answer:</b> <textarea name="answer" rows="3" cols="65" wrap="wrap"></textarea>
	
	<p><b>Category:</b> <select name="cat_name"><option>Select One</option>
	<?php
	$query = "SELECT * FROM kb_categories";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
		echo "<option value=\""$row[0] . "|" . $row[1]"\">$row[1]</option>
		";
	}
	?>
	
	</select></p>
	
	<p><b>Keywords for Search:</b>
	<br /><small>(Use commas to separate keywords/phrases.)</small>
	<br /><textarea name="keywords" rows="2" cols="65" wrap="wrap"></textarea></p>
	
	<p><input type="hidden" name="submitted" value="TRUE" />
	<div align="center"><input type="submit" name="submit" value="Submit Topic" /></p></div>
	</fieldset>
</form>

<?php //Include the HTML footer.

mysql_close(); //Close the database connection.

include ('includes/employfooter.html');
?>
 
d'oh.

this: $list($cid, $cn) = split("|", $_POST['cat_name']);


take out the $ from list
 
got another error.

This error:
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/u3/motorx1/html/kb/employee/add_topic.php on line 127

This is the code:
Code:
<p><b>Category:</b> <select name="cat_name"><option>Select One</option>
	<?php
	$query = "SELECT * FROM kb_categories";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
		echo "<option value=\""$row[0] . "|" . $row[1]"\">$row[1]</option>
		";
	}
	?>

Line 137 is the echo line.
 
got another error.

This error:


This is the code:
Code:
<p><b>Category:</b> <select name="cat_name"><option>Select One</option>
	<?php
	$query = "SELECT * FROM kb_categories";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
		echo "<option value=\""$row[0] . "|" . $row[1]"\">$row[1]</option>
		";
	}
	?>

Line 137 is the echo line.

you have a line break at the end of the line there.

the "; should be right at the end of the </option>
 
Is that the updated code?

Because the quotes inthe echo line are off, as they were before.

the echo should look like:

echo "<option value=\"" . $row[0] . "|" . $row[1] . "\">$row[1]</option>";
 
it should have been. I know I changed those quotes.

Code:
<?php #add_topic.php
//This is the add topic page for MX Employees.

//Include the configuration file for error and management and such.
require_once ('includes/config.inc.php');

//Set the page title and include the HTML header.
$page_title = 'Add a Topic';
include ('includes/employheader.html');

//Set time zone.
date_default_timezone_set("CST6CDT");

//Open the database.
include ('employ_mysql_connect.php');

if (isset($_POST['submitted'])) { //Handle the form.

	require_once ('employ_mysql_connect.php');
	//Connect to the database.
	
	//Check for a topic name.
	if (!empty($_POST['topic_name'])) {
		$tn = escape_data($_POST['topic_name']);
	} else {
		echo '<p><font color="red">You forgot to enter your topic\'s name.</font></p>';
	}
	
	//Check for a topic date.
	if (!empty($_POST['topic_date'])) {
		$td = escape_data($_POST['topic_date']);
	} else {
		echo '<p><font color="red">You forgot to enter your topic\'s date.</font></p>';
	}
	
	//Check for a user.
	if (!empty($_POST['submitted_by'])) {
		$sb = escape_data($_POST['submitted_by']);
	} else {
		echo '<p><font color="red">You forgot to enter your username.</font></p>';
	}
	
	//Check for question.
	if (!empty($_POST['question'])) {
		$q = escape_data($_POST['question']);
	} else {
		echo '<p><font color="red">You forgot to enter your question.</font></p>';
	}
	
	//Check for an answer.
	if (!empty($_POST['answer'])) {
		$a = escape_data($_POST['answer']);
	} else {
		echo '<p><font color="red">You forgot to enter your answer.</font></p>';
	}
	
	//Check for a category.
	if (!empty($_POST['cat_name'])) {
		$c = escape_data($_POST['cat_name']);
	} else {
		echo '<p><font color="red">You forgot to enter your category.</font></p>';
	}
	
	//Check for keywords.
	if (!empty($_POST['keywords'])) {
		$k = escape_data($_POST['keywords']);
	} else {
		echo '<p><font color="red">You forgot to specify keywords and/or phrases.</font></p>';
	}
	
	if ($tn && $td && $sb && $q && $a && $c && $k) { //If everythings ok.
	
		//Split the category information.
		list($cid, $cn) = split("|", $_POST['cat_name']);
	
		//Make sure the topic name is available.
		$query = "SELECT topic_name FROM kb_topics WHERE topic_name='$tn'";
		$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
		
		if (mysql_num_rows($result) == 0) { //Available.
			
			//Add the topic.
			$query = "INSERT INTO kb_topics (topic_name, topic_date, submitted_by, question, answer, cat_name, keywords, cat_id) VALUES ('$tn', '$td', '$sb', '$q', '$a', '$cn', '$k', '$cid')";
			$result = mysql_query ($query) or trigger_error("Query: $query: $query\n<br />MySQL Error: " . mysql_error());
			
			if (mysql_affected_rows() == 1) { //If it ran ok.
				
				//Finish the page.
				echo '<h3>New topic added!</h3>';
				include ('includes/employfooter.html'); //Include the HTML footer.
				exit();
				
			} else { //If it did not run ok.
				echo '<p><font color="red" size="+1">The topic could not be added due to a system error.  Sorry for the inconvenience.</font></p>';
			}
			
		} else { // The topic name is not available.
			echo '<p><font color="red" size="+1">That topic name has already been used.</font></p>';
		}
		
	} else { //If one of the data tests failed.
		echo '<p><font color="red" size="+1">Please try again.</font></p>';
	}
	
} //End of the main Submit conditional.
?>

<h1>Add Topic:</h1>
<form action="add_topic.php" method="post">
	<fieldset>
	
	<p><b>Topic Name:</b> <input type="text" name="topic_name" size="30" maxlength="30" /></p>
	
	<p><b>Topic Date:</b> <input type="text" name="topic_date" size="20" maxlength="20" value="<?php echo date("Y-m-d"); ?>" readonly></p>
	
	<p><b>Submitted By:</b> <input type="text" name="submitted_by" size="35" maxlength="40" value="<?php echo $_SESSION['username']; ?>" readonly></p>
	
	<p><b>Question:</b> <input type="text" name="question" size="65" maxlength="65" /></p>
	
	<p><b>Answer:</b> <textarea name="answer" rows="3" cols="65" wrap="wrap"></textarea>
	
	<p><b>Category:</b> <select name="cat_name"><option>Select One</option>
	<?php
	$query = "SELECT * FROM kb_categories";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
		echo "<option value=\""$row[0] . "|" . $row[1]"\">$row[1]</option>";
	}
	?>
	
	</select></p>
	
	<p><b>Keywords for Search:</b>
	<br /><small>(Use commas to separate keywords/phrases.)</small>
	<br /><textarea name="keywords" rows="2" cols="65" wrap="wrap"></textarea></p>
	
	<p><input type="hidden" name="submitted" value="TRUE" />
	<div align="center"><input type="submit" name="submit" value="Submit Topic" /></p></div>
	</fieldset>
</form>

<?php //Include the HTML footer.

mysql_close(); //Close the database connection.

include ('includes/employfooter.html');
?>

EDIT: Seemed to have missed both dots..hold on..

EDIT 2: Ok, now it loads but when I try to input the information, I get the following:

An error occurred in script '/home/u3/motorx1/html/kb/employee/add_topic.php' on line 74:
split() [function.split]: REG_EMPTY
Date/Time: 7-18-2007 13:27:27

Looking into the db, it is not passing the cat_name or cat_id.
 
Ok, you need to escape the | symbol, so in the split function use "\|"
 
lol. I wouldn't doubt it. Now I've got the task of making the view page work according to my design. whee...the days never end. lol.
 
Back
Top