Jump to content

[SOLVED] Drop Down


peranha

Recommended Posts

I am trying to get a site up and running.  I have 4 tables.  A camera table, a facility table, a misc table,and a table that has foreign keys to each of these 3 tables.  I have a dropdown list and need to input the id from each of the tables into the corresponding columns of the last table.  I can put the page online if needed.  Like I said the drop downs work, but the insert doesnt insert the id of what the user selects.  If anyone can help that would be great.  I can give as much info as needed, and even export the sql statements if needed, and the PHP script I have so far.
Thanks in advance for the help.
Link to comment
Share on other sites

Here is the code.

<HTML>
<HEAD><TITLE>Test Page</TITLE>
</HEAD>
<BODY>

<form action="dropdownupload.php" method="post">
<input type="submit" name="submit">

<?PHP
// set server access variables
$host = "localhost";
$user = "user";
$pass = "password";
$db = "equipment";

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

//this code is bringing in the values for the dropdown.
$query="SELECT * FROM facilities";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=\"facilities\" value=''>Facilities Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=\"$nt[facilityid]\">$nt[facilityname]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

//this code is bringing in the values for the dropdown.
$query="SELECT * FROM cameras";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=\"cameras\" value=''>Cameras model</option>";
// printing the list box select command

while($nt1=mysql_fetch_array($result)){//Array or records stored in $nt1
echo "<option value=$nt1[cameraid]>$nt1[model]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

//this code is bringing in the values for the dropdown.
$query="SELECT * FROM misc";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=\"misc\" value=''>misc building</option>";
// printing the list box select command

while($nt2=mysql_fetch_array($result)){//Array or records stored in $nt2
echo "<option value=$nt2[miscid]>$nt2[building]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

// create query
$query = "INSERT INTO facility_cameras (facilityid, cameraid, miscid) VALUES ('$nt', '$nt1', '$nt2')";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// close connection
mysql_close($connection);
?>
</form>

</BODY>
</HTML>
Link to comment
Share on other sites

Your Insert statement is incorrect. The statement should look something like:

$query = "INSERT INTO facility_cameras (facilityid, cameraid, miscid) VALUES ("$_POST['nt']", "$_POST['$nt1']", "$_POST['$nt2']")";

PHP cannot process form data unless it was submitted from a previous request since all its processing is server side. Also, You should think about separating the form and processing pages or at least throw a conditional in there or else you will be making an empty INSERT request every time the page loads when you only want that happening when someone submits a page.

B
Link to comment
Share on other sites

I tried this, but had no luck working.  I added a $ in the first NT field, and no luck, so I tried it without the $ signs in it at all, and no luck.  I will try to split the pages up when I get this working on one page though.  I never thought that it would make an empty insert request.  When I try to load it, it just comes up with a blank white page, no errors.  If I comment out the $query line, it loads the page fine.  Here are the last lines of the code.  The rest stayed the same.

echo "</select>";// Closing of list box

// create query
$query = "INSERT INTO facility_cameras (facilityid, cameraid, miscid) VALUES ("$_POST['$nt']", "$_POST['$nt1']", "$_POST['$nt2']")";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// close connection
mysql_close($connection);

Thanks again.
Link to comment
Share on other sites

Sorry I forgot to remove the "$" from within the $_POST[] variable. Remove those $'s and it should be good. So:

$query = 'INSERT INTO facility_cameras (facilityid, cameraid, miscid) VALUES ("$_POST["nt"]", "$_POST["nt1"]", "$_POST["nt2"]")';

If it still doesn't work, then check you error log file to see the exact error. If you are *nix: tail /var/log/messages should do it.

Sorry, just woke up, still not thinking clearly yet!!! Notice I changed the " and ' in the statement as well.

B
Link to comment
Share on other sites

Did this, and still no luck.  This is the error I get when loading the web page.

Error in query: INSERT INTO facility_cameras (facilityid, cameraid, miscid) VALUES ("$_POST["nt"]", "$_POST["nt1"]", "$_POST["nt2"]"). You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'nt"]", "$_POST["nt1"]", "$_POST["nt2"]")' at line 1
Link to comment
Share on other sites

I must be really off today, this should be a very easy thing. Try this:

$query = "INSERT INTO facility_cameras (facilityid, cameraid, miscid) VALUES ('{$_POST['nt']}', '{$_POST['nt1']}', '{$_POST['nt2']}')";

I usually filter my POST and GET variables thus changing the name so I don't have to deal with so many "s and 's..I've forgotten how to deal with them. The curly braces tell PHP that the enclosed data is a variable and it should replace everything with the proper values before it gets to MySQL.

Give it a shot, if not, then I must be completely off the deep end today. If MySQL still complains, then ditch the {} and the 's inside the $_POST variable. Technically you are supposed to have them (or "s) but it will work without them.
Link to comment
Share on other sites

I tried this, at no luck.  Here is the error I am getting if it helps any.  This is all new to me, but it looks as though it is not getting the ID for some reason.

Error in query: INSERT INTO facility_cameras (facilityid, cameraid, miscid) VALUES ('', '', ''). Out of range value adjusted for column 'facilityid' at row 1

Link to comment
Share on other sites

You could also try:
$query = "INSERT INTO facility_cameras (facilityid, cameraid, miscid) VALUES ('".$_POST['nt']."', '".$_POST['nt1']."', '".$_POST['nt2']."')";

run print_r($_POST) at the top to make sure the post vars are being set as you think they should be.
Link to comment
Share on other sites

This is all new to me, I just started with PHP, MYSQL about 2 weeks ago, and am still getting the hang of it.  When I put in the print_r($_POST) at the beginning of the PHP code, I just get a blank white screen.  Is there any thing else I need to put in front of it or behind. 
Link to comment
Share on other sites

OK, I did the print, and here is what I get when I select different dropdown items in the web page.  It changes to correspond with the item selected.  Array ( [submit] => Submit Query [facilities] => 8 [cameras] => 4 [misc] => 9 )  This is with the following.

// create query
//$query = "INSERT INTO facility_cameras (facilityid, cameraid, miscid) VALUES ("$_POST['$nt']", "$_POST['$nt1']", "$_POST['$nt2']")";

// execute query
//$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

Thanks for the help and the patients.
Link to comment
Share on other sites

Changed the code to look like this, and tried all that I can think of at no luck of it working.

<?PHP
// set server access variables
$host = "localhost";
$user = "user";
$pass = "passwork";
$db = "equipment";

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

//this code is bringing in the values for the dropdown.
$query="SELECT * FROM facilities";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=\"facilities\" value=''>Facilities Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=\"$nt[facilityid]\">$nt[facilityname]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

//this code is bringing in the values for the dropdown.
$query1="SELECT * FROM cameras";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result1 = mysql_query ($query1);
echo "<select name=\"cameras\" value=''>Cameras model</option>";
// printing the list box select command

while($nt1=mysql_fetch_array($result1)){//Array or records stored in $nt1
echo "<option value=$nt1[cameraid]>$nt1[model]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

//this code is bringing in the values for the dropdown.
$query2="SELECT * FROM misc";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result2 = mysql_query ($query2);
echo "<select name=\"misc\" value=''>misc building</option>";
// printing the list box select command

while($nt2=mysql_fetch_array($result2)){//Array or records stored in $nt2
echo "<option value=$nt2[miscid]>$nt2[building]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

// create query
//$query = "INSERT INTO facility_cameras (facilityid, cameraid, miscid) VALUES ('".$_POST['nt']."', '".$_POST['nt1']."', '".$_POST['nt2']."')";

// execute query
//$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

$query4 = print_r($_POST);

// close connection
mysql_close($connection);
?>

Every time I uncomment the $query Insert line, the page doesnt load, and this is what shows up in my Apache error log.  Undefined index:  nt, nt1, nt2.
Link to comment
Share on other sites

$_POST is an array. The KEYs in it are the NAMES of your INPUTS. None of your inputs have name="n1" so you can't see $_POST['n1'] because it doesn't EXIST.

When you did print_r($_POST) it showed you the KEYS and VALUES. The KEYS are the names of your inputs.
I don't know how to make it clearer.

<input type="text" name="foo" value="bar" />
Will result in $_POST['foo'] being = to "bar";
You can't access a variable that doesn't exist.

Link to comment
Share on other sites

[quote author=peranha link=topic=122745.msg507465#msg507465 date=1169083955]
echo "<select name=\"facilities\" value=''>Facilities Name</option>";
echo "<select name=\"cameras\" value=''>Cameras model</option>";
echo "<select name=\"misc\" value=''>misc building</option>";
[/quote]

1 .The name of a select is what's inside of name="".
2. Select doesn't have a value="". The value goes on the <option>s only.
3. You should use single quotes on strings which do not have variables. These lines and many of your others should be changed:
[code]echo '<select name="facilities">Facilities Name</option>';[/code]

When you do print_r($_POST) you should see keys of facilities, cameras, and misc. THOSE are the posted variables.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.