Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
simple insert w/ dropdown problem
Old 05-23-2005, 02:50 PM simple insert w/ dropdown problem
Junior Talker

Posts: 1
Trades: 0
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/artic...07_06_04a.html

Here's my code:

Code:
<?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 = "INSERT INTO cmsdevices (companyID,device,desc,connect,features,thearticle) VALUES (".
"'".$HTTP_POST_VARS['companyID']."', ".
"'".$HTTP_POST_VARS['device']."', ".
"'".$HTTP_POST_VARS['desc']."', ".
"'".$HTTP_POST_VARS['connect']."', ".
"'".$HTTP_POST_VARS['features']."', ".
"'".$HTTP_POST_VARS['thearticle']."')";  
}
// 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="multipart/form-data" name="Devices" method="POST" action="newdevice.php">
		  <div class="row"><span class="label">
		  <?php
		  $sql="SELECT id,company FROM cmscompany ORDER BY company";
		  $result=mysql_query($sql);

		  $options="";

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

		  $id=$row["id"];
		  $company=$row["company"];
		  $options.="<option value=\"$id\">".$company.'</option>';
		  }
		  ?>
		  <select name="companyID" id="companyID" /><option value="0">Choose Company<?=$options?></select></span></div>
          <div class="row"><span class="label">Device:</span><span class="formw"><input name="device" type="text" size="35" id="device" /></span></div>
          <div class="row"><span class="label">Description:</span><span class="formw"><textarea name="desc" cols="30" rows="6" id="desc"></textarea></span></div>
          <div class="row"><span class="label">Connectivity:</span><span class="formw"><input name="connect" type="text" size="35" id="connect" /></span></div>
          <div class="row"><span class="label">Features:</span><span class="formw"><textarea name="features" cols="30" rows="6" id="features"></textarea></span></div>
          <div class="row"><span class="label">Specifications:</span><span class="formw"><textarea name="thearticle" cols="30" rows="6" id="thearticle"></textarea></span></div>
          <div class="row"><span class="formw"><input type="submit" name="action" value="Submit" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Reset Form" /></span></div>
          <div class="spacer">&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
Superfrappe is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-23-2005, 07:21 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
Right o then.

For a start, $HTTP_POST_VARS is redundent now, we don't use it in PHP anymore. Instead it should simply be $_POST (or $_GET depending on the method). Let's also clear up that bloody nasty looking INSERT command!

PHP Code:
if ($_POST[action]) {

// Open SQL Portal
   
$link mysql_pconnect("sql_host""sql_user""sql_pass")
       or die(
"Could not connect: " mysql_error());
       
// Open Specified Database
   
mysql_select_db("databasename")
       or die(
"Could not select database: " mysql_error());

$insertQuery "INSERT INTO cmsdevices SET 
companyID='
$_POST[companyID]',
device='
$_POST[device]',
desc='
$_POST[desc]',
connect='
$_POST[connect]',
features='
$_POST[features]',
thearticle='
$_POST[thearticle]'";

$result mysql_query($query)
          or die(
"Query failed: " mysql_error());

echo 
'<p><center>Company added to the database</center>';


This should replace the section of PHP above your HTML output. Please add in the values for the mysql_pconnect(); and mysql_select_db(); otherwise it won't connect to the database.

This should work with no problems at all.

If you learnt all that from the tutorial, it's rubbish! Check out www.sitepoint.com
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to simple insert w/ dropdown problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.34811 seconds with 12 queries