bullbreed Posted February 8, 2010 Share Posted February 8, 2010 Hi Guys. I got a little help about this yesterday but still a bit lost. I have a news article system on my website that i want to sort by article_section The user has an option to either create a section for an article or select a section from a list if one already exists. I put 2 form elements on the page. One is an input text field called article_section so the user can add a section. Another is a menu field called existingarticle_section so the user can select one from the list. What I want to do is something like Check if article_section has a value. If it does then create a section with this value if it doesn't the check existingarticle_section If existingarticle_section has a value, put the article in this section. However I have a default value in the existingarticle_section of "None" I dont know how to check the values and add the right one n to the database. I'm new to php and having difficulty writing this coz i just don't know any help would be great. Here is the PHP <?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>"); } } } ?> and 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="existingarticle_section" id="existingarticle_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> Thanks guys in advance Bullbreed Link to comment https://forums.phpfreaks.com/topic/191367-if-else-statements/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.