Jump to content


Help finishing MySQL query.

  • This topic is locked This topic is locked
1 reply to this topic

#1 Perad

  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 17 October 2006 - 10:30 AM

I am really close to finishing this off.

The problem is i don't have a clue how to do it.

I think the problem is that i am going about it in the wrong way. Anyway the first function creates a form.

	function add_article () {
			echo "Category:<br /><select name=\"category\">";
					global $db;
					$query = "SELECT category_id, category_name FROM article_category ORDER BY category_id";
					$result = mysql_query ($query) or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error()); 
					while ($row = mysql_fetch_assoc ($result)) {
					 /* display news in a simple table */
					 /* place table row data in 
					 * easier to use variables.
					 * Here we also make sure no
					 * HTML tags, other than the
					 * ones we want are displayed */
					$cat_id = ($row['category_id']);
					$cat_name = htmlentities ($row['category_name']);       
					/* display the data */
					echo "<option>" . $cat_id . "-" . $cat_name . "</option>";
			echo "</select>";
			/*End of Select Category*/

The second uses the first function and enters data into the database when the submit button is pressed. The problem is with the last entry 'category_id'. What i am trying to do is allow the site administrator to select the category they want from above. Then when they press submit, it will take the category id of the dropdown box and insert it into the database. Unfortunately its just not happening.

With the way my code is set-up is it even possible to do what i want? If not could you give me advice on how i would set it up to make it work.

Second Function

function handle_article () {	
			/*Start of new form*/
				if (isset($_POST['add_article'])) {
					$message = NULL; // Create an empty new variable.
						if (empty($_POST['articletitle'])) {
							$at = FALSE;
							$message .= '<p>You forgot to enter the article title!</p>';
						} else {
							$at = $_POST['articletitle'];
						if (empty($_POST['articletext'])) {
							$atext = FALSE;
							$message .= '<p>You forgot to enter the article text!</p>';
						} else {
							$atext = $_POST['articletext'];
						if ($at && $atext ) {
							global $db;
							// Make the query.
							$query = "INSERT INTO articles (postdate, title, articletext, category_id) VALUES(NOW(), '$at', '$atext', '$cat_id')";
							$result = mysql_query($query);
									if ($result) { // If it ran OK.
										// Confirmation.
										echo '<p><b>A new category has been added!</b></p>';
										exit(); // Quit the script.		
									} else { // If it did not run OK.
										$message = '<p>The category couldn\'t be added. If the problem persists please e-mail the administrator.</p><p>' . mysql_error()
										. '</p>'; 
						mysql_close(); // Close the database connection.
						} else {
							$message .= '<p>Please try again.</p>';		
		if (isset($message)) {
		echo '<font color="red">'. $message. '</font>';

Thanks for any help given


#2 Perad

  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 17 October 2006 - 11:48 AM

Is something unclear? Or is the lack of replies simply because people don't know.

If you want me to try to rephrase it i will.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users