Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Inserting data into a database

Posted by jennww (jennww), 6 December 2005
Hi All,

I am trying to create a form for adding a new record to my database but I would like to be able to have some of the fields to have the function of selecting from the database if already there or to create new if not. For example project category are in a table called track_categorys, I have called a list from this table and inserted into a drop down box with the value as its id.
code for box;
<p> Select:
   <input name="category" type="radio" value="existing" />
               <select name="existing" class="maintext">
                 <option>Please Select</option>
<?php // Retrieve all the categories
 $query = "SELECT category_id, category FROM track_categorys";            
$result = @mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
     echo "<option value=\"{$row['category_id']}\">{$row['category']}</option>\n";
}
?>
</select> </p>
     <p>New Category :
     <input name="category" type="radio" value="new" />  
      <input name="category" type="text" class="maintext" size="20" maxlength="20" /></p>

This works when I check it live.

So the php bit
if ($_POST['category'] == 'new') {
  // If it's a new category, add the category to the database.
$query = 'INSERT INTO track_categorys (category_id, category) VALUES (NULL, ';            
  // Check for a category.
if (!empty($_POST['category'])) {
$query .= "'" . escape_data($_POST['category']) . "')";
$result = mysql_query ($query); // Run the query.
$cp = mysql_insert_id(); // Get the category ID.            
           } else {
                 $cp = FALSE;
                 echo '<p>Please enter the a new category!</p>';
           }            
     } elseif (($_POST['category'] == 'existing') && ($_POST['existing'] > 0)) {
           $cp = $_POST['existing'];
     } else {
           $cp = FALSE;
           echo '<p>Please enter or select a category!</p>';
     }

When I run the form I received data to my database from my simple input fields, however this section just returns 'Please enter or select a category!'
Please could you make any sugestions.

Posted by admin (Graham Ellis), 7 December 2005
I think you have a logic flaw there ... your else clauses aren't quite as I would expect them so that the ONLY way you'll pass into most of your logic is if the category is 'new'.   So then there's no point in doing other checks on that variable inside.

Posted by jennww (jennww), 7 December 2005
Hi Graham,

I had also posted in another forum. They have pointed out that I have my new category text box is called the same as my radio buttons.
I have now change this category_n in both the form and php sections and it is working.

HTML code;
<p> Select:
   <input name="category" type="radio" value="existing" />
     <select name="existing" class="maintext">
  <option>Please Select</option>
<?php // Retrieve all the categories
 $query = "SELECT category_id, category FROM track_categorys";  
$result = @mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<option value=\"{$row['category_id']}\">{$row['category']}</option>\n";
}
?>
</select> </p>
<p>New Category :
<input name="category" type="radio" value="new" />  
 <input name="category_n" type="text" class="maintext" size="20" maxlength="20" /></p>

the php bit
if ($_POST['category'] == 'new') {
  // If it's a new category, add the category to the database.
$query = 'INSERT INTO track_categorys (category_id, category) VALUES (NULL, ';  
  // Check for a category.
if (!empty($_POST['category_n'])) {
$query .= "'" . escape_data($_POST['category_n']) . "')";
$result = mysql_query ($query); // Run the query.
$cp = mysql_insert_id(); // Get the category ID.  
 } else {
  $cp = FALSE;
  echo '<p>Please enter the a new category!</p>';
 }  
} elseif (($_POST['category'] == 'existing') && ($_POST['existing'] > 0)) {
 $cp = $_POST['existing'];
} else {
 $cp = FALSE;
 echo '<p>Please enter or select a category!</p>';
}

Thank you for your reply.


Posted by admin (Graham Ellis), 7 December 2005
on 12/07/05 at 12:33:08, jennww wrote:
Hi Graham,

I had also posted in another forum. They have pointed out that ...


Glad you have it sorted.  Would much appreciate it if people could post back when problems have been sorted elsewhere as I've got better things to do than reply to questions that have alread been answered ...  

But thanks for letting me know  

Posted by jennww (jennww), 7 December 2005
Please may I say I had only just recieved the solution from the other forum before you posted.
It was also a forum I had not used before and I have experienced being ignored in new forums.
You have help me before, so thats why I had asked.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho