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:
Any help is as always appreciative. Any extra info need is available upon request. Thanks!
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!