bullbreed Posted February 7, 2010 Share Posted February 7, 2010 Hi. Newbie PHP here. I am trying to learn PHP by developing a CMS I am part way through it but came accross this problem. I am creating a News Article system in the CMS and I want the articles to be associated with a category. This is so when a user clicks on the NEWS button, all the articles will be sorted by category; I'm not sure how to do this so I did the following. 1. Created a column in the database called article_category 2. Created a form Input text box so the user can create a category if it doesnt exist. 3. Created a Input menu field that needs to be populated with existing categories. I know i cant use the same name for these 3 form elements but I dont know how to set this up. How do I check if the Text Box is filled in to create a category or if a value from the Menu field has been selected and how to put the selected value in to the database. Heres the code I have so far <?php $submit = $_POST['submit']; //Form data $articlesection = mysql_real_escape_string($_POST['article_section']); $articletitle = mysql_real_escape_string($_POST['article_title']); $articleptitle = mysql_real_escape_string($_POST['article_ptitle']); $articlepdescription = mysql_real_escape_string($_POST['article_pdescription']); $articlepkeywords = mysql_real_escape_string($_POST['article_pkeywords']); $articlecontent = mysql_real_escape_string($_POST['article_content']); $date = date('d/m/Y \a\t g:i.s a'); if ($submit){ //Open the database mysql_connect("localhost","root",""); mysql_select_db("*************"); //Select the database $pagecheck = mysql_query("SELECT `article_title` FROM articles WHERE `article_title` = '$articletitle'");echo mysql_error(); $count = mysql_num_rows($pagecheck);echo mysql_error(); if ($count != 0){ echo("<font size=\"2\" color=\"#ff0000\">Article already exists. Please edit the existing article!</font>");echo mysql_error(); }else{ //Check for existing fields if ($articlesection && $articletitle && $articleptitle && $articlepdescription && articlepkeywords && $articlecontent && $date){ //Enter into Database $queryreg = mysql_query("INSERT INTO articles (`article_section`, `article_title`, `article_ptitle`, `article_pdescription`, `article_pkeywords`, `article_content`, `date`) VALUES ('$articlesection', '$articletitle', '$articleptitle', '$articlepdescription', '$articlepkeywords', '$articlecontent', '$date')"); echo("<font size=\"2\" color=\"#00cc00\">Your article has been created! </font>"); }else{ echo ("<font size=\"2\" color=\"#ff0000\">Please complete <b>ALL</b> fields</font>"); } } } ?> Here is the form <form action="addarticle.php" method="post" enctype="multipart/form-data" name="addarticle" target="_self" id="addarticle"> <!-- Form Starts --> <div class="titlearea">Add News Article</div> <div class="dataarea"> <div class="infowrap">Article title</div> <div class="infowrap">Create Article Section</div> <div class="infowrap">Select Article Section</div> <div class="infowrap"><input name="article_title" type="text" id="article_title" size="25" maxlength="20" /> </div> <div class="infowrap"><input name="article_section" type="text" id="article_section" size="25" maxlength="100" /> </div> <div class="infowrap"> <label></label> <label> <select name="article_section" id="article_section"> <option><?php echo $row["article_section"]; ?></option> </select> </label> </div> </div> <div class="titlearea">Meta Data</div> <div class="dataarea"> <div class="metawrap">Title - Please use approx 65 characters</div> <div class="metawrap"><input name="article_ptitle" type="text" id="article_ptitle" size="80" maxlength="20" /> </div> <div class="metawrap">Description - Please use approx 155 characters inc spaces</div> <div class="metawrap"><input name="article_pdescription" type="text" id="article_pdescription" size="80" maxlength="200" /> </div> <div class="metawrap">Keywords - Please use approx 5 keyword per page</div> <div class="metawrap"><input name="article_pkeywords" type="text" id="article_pkeywords" size="80" maxlength="200" /> </div> </div> <div class="titlearea">Article Content</div> <div class="dataarea"> <textarea name="article_content" cols="80" rows="20" id="article_content"></textarea> </div> <div class="dataarea"> <input type="submit" value="Save" name="submit" /></div> </form> Any ideas would be a great help Link to comment https://forums.phpfreaks.com/topic/191271-can-i-have-2-form-fields-that-subit-data-to-same-column-in-db/ Share on other sites More sharing options...
bullbreed Posted February 7, 2010 Author Share Posted February 7, 2010 Anyone? Link to comment https://forums.phpfreaks.com/topic/191271-can-i-have-2-form-fields-that-subit-data-to-same-column-in-db/#findComment-1008525 Share on other sites More sharing options...
teamatomic Posted February 7, 2010 Share Posted February 7, 2010 ORDER BY birth DESC SELECT article_title, category FROM articles WHERE article_title = $articletitle ORDER BY category DESC); HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/191271-can-i-have-2-form-fields-that-subit-data-to-same-column-in-db/#findComment-1008531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.