bullbreed Posted February 9, 2010 Share Posted February 9, 2010 Hi. I'm trying to check 2 variables from a form. If one variable exists then put the value in the article_section column of the database If it doesnt have a value then put the value of a different variable i to the article_section column of the database I tried this if ($existcat !== ""){ $queryreg = mysql_query("INSERT INTO `articles` (`article_section`) VALUES ('$existcat')");echo mysql_error(); }else{ $queryreg = mysql_query("INSERT INTO `articles` (`article_section`) VALUES ('$newcat')");echo mysql_error(); } The code is actually longer than this and checks other variables from the form like aticle content, title etc But the part of the code aove puts 2 rows in to the database. One row with only the article_category and no other article information One row with the article information with no article_category How do I check 2 variables and select which one I want to put in the particular database column 'article_section' Thanks Quote Link to comment https://forums.phpfreaks.com/topic/191510-checking-2-variables-and-entering-only-one-in-to-database/ Share on other sites More sharing options...
yozyk Posted February 9, 2010 Share Posted February 9, 2010 $cat = empty($existcat)? $newcat:$existcat; $queryreg = mysql_query("INSERT INTO `articles` (`article_section`) VALUES ('$cat')"); Quote Link to comment https://forums.phpfreaks.com/topic/191510-checking-2-variables-and-entering-only-one-in-to-database/#findComment-1009544 Share on other sites More sharing options...
bullbreed Posted February 9, 2010 Author Share Posted February 9, 2010 Thank you but I still get the same. 2 rows. One with the article_section only and another with all the article information. Strange one this is Quote Link to comment https://forums.phpfreaks.com/topic/191510-checking-2-variables-and-entering-only-one-in-to-database/#findComment-1009550 Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 that doesn't make any sense. are you sure you are getting 2 new rows from that insert code that yoz gave you? doesn't seem possible to me. perhaps you should post the rest of your code Quote Link to comment https://forums.phpfreaks.com/topic/191510-checking-2-variables-and-entering-only-one-in-to-database/#findComment-1009553 Share on other sites More sharing options...
CoolGeek Posted February 9, 2010 Share Posted February 9, 2010 if (!empty($existcat)){ $queryreg = mysql_query("INSERT INTO `articles` (`article_section`) VALUES ('$existcat')");echo mysql_error(); }else{ $queryreg = mysql_query("INSERT INTO `articles` (`article_section`) VALUES ('$newcat')");echo mysql_error(); } it should work... Quote Link to comment https://forums.phpfreaks.com/topic/191510-checking-2-variables-and-entering-only-one-in-to-database/#findComment-1009554 Share on other sites More sharing options...
bullbreed Posted February 9, 2010 Author Share Posted February 9, 2010 It is probably my code because i've only been learning php for a month or so. Also tried Cool Geeks example as well and still got 2 rows. Heres the entire code <?php $submit = $_POST['submit']; //Form data $existcat = mysql_real_escape_string($_POST['exist_cat']); $newcat = mysql_real_escape_string($_POST['new_cat']); $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 (isset($_POST['submit'])) { //Open the database mysql_connect("localhost","root",""); mysql_select_db("*********"); //Select the database $cat = empty($existcat)? $newcat:$existcat; $queryreg = mysql_query("INSERT INTO `articles` (`article_section`) VALUES ('$cat')"); //Check for existing fields if ($articletitle && $articleptitle && $articlepdescription && articlepkeywords && $articlecontent && $date){ $pagecheck = mysql_query("SELECT `article_title` FROM articles WHERE `article_title` = '$articletitle'");echo mysql_error(); $count = mysql_num_rows($pagecheck); if ($count != 0){ echo("<font size=\"2\" color=\"#ff0000\">Article already exists. Please edit the existing article!</font>");echo mysql_error(); }else{ //Enter into Database $queryreg = mysql_query("INSERT INTO articles (`article_title`, `article_ptitle`, `article_pdescription`, `article_pkeywords`, `article_content`, `date`) VALUES ('$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>"); } } //end Check for existing fields //end if submit $sql = "SELECT * FROM `articles` ORDER BY `id` ASC"; $query = mysql_query($sql); ?> </div> <!-- Page submission Result --> <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="new_cat" type="text" id="new_cat" size="25" maxlength="100" /> </div> <div class="infowrap"> <?php //Open the database mysql_connect("localhost","root",""); mysql_select_db("customfight"); //Select the database $sql = "SELECT DISTINCT(article_section) FROM articles"; $query = mysql_query($sql)or die;(mysql_error()); if(mysql_num_rows($query) > 0){ ?> <label> <select name="exist_cat" id="exist_cat"> <option selected></option> <?php while($r = mysql_fetch_array($query)){ echo '<option value=" ' .$r['article_section'] . '">' . $r['article_section'] . '</option>'; } ?> </select> <?php } ?> </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> Quote Link to comment https://forums.phpfreaks.com/topic/191510-checking-2-variables-and-entering-only-one-in-to-database/#findComment-1009558 Share on other sites More sharing options...
bullbreed Posted February 9, 2010 Author Share Posted February 9, 2010 Sorted it. I'm not sure why 2 rows were being created bit I changed the code to read; if ($existcat !== ""){ $cat = $existcat; }else{ $cat = $newcat; } Then added $cat to the INSERT function further down the page to combine the INSERT function. Thanks for your help everyone. Thanks for the answer wide_load and libeco Quote Link to comment https://forums.phpfreaks.com/topic/191510-checking-2-variables-and-entering-only-one-in-to-database/#findComment-1009579 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.