Page 1 of 1

simple insert w/ dropdown problem

Posted: Mon May 23, 2005 11:06 am
by Superfrappe
Howdy. I've got a page with your typical INSERT going on, but I have a feeling my dropdown select is interfering with the values being inserted. Basically, there is a dropdown to select a company, then I just want to be able to insert the values into the table. I have one table called cmscompany, and another called cmsdevices. The company table works fine, and I currently have about 300 companies in there. I just can't seem to get the newdevice page to insert any data into the cmsdevices table.

My fields in the cmsdevices table are as follows:

ID (primary)
companyID
device
desc
connect
features
thearticle

This code is based from the article written by Peter Zeidman http://www.intranetjournal.com/articles/200407/ij_07_06_04a.html

Here's my code:

Code: Select all

<?php

// --- data entry code
// Get the PHP file containing the DbConnector class
require_once('../includes/DbConnector.php');

// Check whether a form has been submitted. If so, carry on
if ($HTTP_POST_VARS){

// Create an instance of DbConnector
$connector = new DbConnector();

// Create an SQL query (MySQL version)
$insertQuery = &quote;INSERT INTO cmsdevices (companyID,device,desc,connect,features,thearticle) VALUES (&quote;.
&quote;'&quote;.$HTTP_POST_VARS&#1111;'companyID'].&quote;', &quote;.
&quote;'&quote;.$HTTP_POST_VARS&#1111;'device'].&quote;', &quote;.
&quote;'&quote;.$HTTP_POST_VARS&#1111;'desc'].&quote;', &quote;.
&quote;'&quote;.$HTTP_POST_VARS&#1111;'connect'].&quote;', &quote;.
&quote;'&quote;.$HTTP_POST_VARS&#1111;'features'].&quote;', &quote;.
&quote;'&quote;.$HTTP_POST_VARS&#1111;'thearticle'].&quote;')&quote;;  
}
// Save the form data into the database 
if ($result = $connector->query($insertQuery)){
// It worked, give confirmation
echo '<p><center>Company added to the database</center>';
}else{
// It hasn't worked so stop. Better error handling code would be good here!
exit('<p><center>Sorry, there was an error saving to the database</center>');
}
?>


        <form enctype=&quote;multipart/form-data&quote; name=&quote;Devices&quote; method=&quote;POST&quote; action=&quote;newdevice.php&quote;>
		  <div class=&quote;row&quote;><span class=&quote;label&quote;>
		  <?php
		  $sql=&quote;SELECT id,company FROM cmscompany ORDER BY company&quote;;
		  $result=mysql_query($sql);

		  $options=&quote;&quote;;

		  while ($row=mysql_fetch_array($result)) {

		  $id=$row&#1111;&quote;id&quote;];
		  $company=$row&#1111;&quote;company&quote;];
		  $options.=&quote;<option value=\&quote;$id\&quote;>&quote;.$company.'</option>';
		  }
		  ?>
		  <select name=&quote;companyID&quote; id=&quote;companyID&quote; /><option value=&quote;0&quote;>Choose Company<?=$options?></select></span></div>
          <div class=&quote;row&quote;><span class=&quote;label&quote;>Device:</span><span class=&quote;formw&quote;><input name=&quote;device&quote; type=&quote;text&quote; size=&quote;35&quote; id=&quote;device&quote; /></span></div>
          <div class=&quote;row&quote;><span class=&quote;label&quote;>Description:</span><span class=&quote;formw&quote;><textarea name=&quote;desc&quote; cols=&quote;30&quote; rows=&quote;6&quote; id=&quote;desc&quote;></textarea></span></div>
          <div class=&quote;row&quote;><span class=&quote;label&quote;>Connectivity:</span><span class=&quote;formw&quote;><input name=&quote;connect&quote; type=&quote;text&quote; size=&quote;35&quote; id=&quote;connect&quote; /></span></div>
          <div class=&quote;row&quote;><span class=&quote;label&quote;>Features:</span><span class=&quote;formw&quote;><textarea name=&quote;features&quote; cols=&quote;30&quote; rows=&quote;6&quote; id=&quote;features&quote;></textarea></span></div>
          <div class=&quote;row&quote;><span class=&quote;label&quote;>Specifications:</span><span class=&quote;formw&quote;><textarea name=&quote;thearticle&quote; cols=&quote;30&quote; rows=&quote;6&quote; id=&quote;thearticle&quote;></textarea></span></div>
          <div class=&quote;row&quote;><span class=&quote;formw&quote;><input type=&quote;submit&quote; name=&quote;action&quote; value=&quote;Submit&quote; />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=&quote;reset&quote; value=&quote;Reset Form&quote; /></span></div>
          <div class=&quote;spacer&quote;>&nbsp;</div>
        </form>


I've gone over my code for over a week now and I just can't get this to work. It's the very last step I need to do to finish this project, and I'm just stuck. Any advice, help, or even just a comment would really help me out. Thank you all in advance!

Cheers,
Rob

Posted: Mon May 23, 2005 11:19 am
by phpScott
are you getting any sort of error.

try echoing out $insertQuery to see what you query looks like.
run it agians your db manually or using phpMyAdmin to see if there are any errors in the syntax.

Posted: Mon May 23, 2005 12:11 pm
by Superfrappe
Hey phpScott, thanks for your quick response. Does my select code in the form look right to you? This is the first time I've used a drop down select with PHP and I'm not quite sure if I did it right. I'm using phpMyAdmin, so is there an error log in phpMyAdmin? Thank you again for your time!

Posted: Mon May 23, 2005 2:23 pm
by phpScott
it looks ok except where you start your select the first option should be like

Code: Select all

<select name=&quote;companyID&quote; id=&quote;companyID&quote; /><option value=&quote;0&quote;>Choose Company</option>
you just forgot to close the first option.

do a view source when you first run the page and make sure that the select looks rigth.

Code: Select all

<select name=&quote;companyID&quote; id=&quote;companyID&quote; /><option value=&quote;0&quote;>Choose Company</option><option value=&quote;1&quote;>Company xyz</option></select>
great you have phpMyAdmin, no it doesn't monitor the mysql db but you can use it to quickly check your query and play with it there to get the query right before installing it into your code. Saves alot of change, save, run, change, save, run.

If correcting the select statement doesn't fix the problem do like a previously posted and echo out $insertQuery then cut and paste it into a phpMyAdmin query window and see if you can get it inserting properly.