peranha Posted January 17, 2007 Share Posted January 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
linuxdream Posted January 17, 2007 Share Posted January 17, 2007 If you need specific answers you really need to post at least the main portions of your code. Or else how can we help? Quote Link to comment Share on other sites More sharing options...
peranha Posted January 17, 2007 Author Share Posted January 17, 2007 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 databasemysql_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 commandwhile($nt=mysql_fetch_array($result)){//Array or records stored in $ntecho "<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 commandwhile($nt1=mysql_fetch_array($result)){//Array or records stored in $nt1echo "<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 commandwhile($nt2=mysql_fetch_array($result)){//Array or records stored in $nt2echo "<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 connectionmysql_close($connection);?></form></BODY></HTML> Quote Link to comment Share on other sites More sharing options...
linuxdream Posted January 17, 2007 Share Posted January 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
peranha Posted January 17, 2007 Author Share Posted January 17, 2007 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 connectionmysql_close($connection);Thanks again. Quote Link to comment Share on other sites More sharing options...
linuxdream Posted January 17, 2007 Share Posted January 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
peranha Posted January 17, 2007 Author Share Posted January 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
linuxdream Posted January 17, 2007 Share Posted January 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
peranha Posted January 17, 2007 Author Share Posted January 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 17, 2007 Share Posted January 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
peranha Posted January 17, 2007 Author Share Posted January 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 17, 2007 Share Posted January 17, 2007 You need to end it with the same syntax you end any statement with, a ; Quote Link to comment Share on other sites More sharing options...
peranha Posted January 17, 2007 Author Share Posted January 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 17, 2007 Share Posted January 17, 2007 so you can't use $_POST['nt'] because it doesn't exist. You need to check your variable names and the form. Quote Link to comment Share on other sites More sharing options...
peranha Posted January 18, 2007 Author Share Posted January 18, 2007 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 databasemysql_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 commandwhile($nt=mysql_fetch_array($result)){//Array or records stored in $ntecho "<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 commandwhile($nt1=mysql_fetch_array($result1)){//Array or records stored in $nt1echo "<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 commandwhile($nt2=mysql_fetch_array($result2)){//Array or records stored in $nt2echo "<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 connectionmysql_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. Quote Link to comment Share on other sites More sharing options...
peranha Posted January 20, 2007 Author Share Posted January 20, 2007 I am trying to figure this out, but cannot. If you could give an example, that would be great. Thanks for the help so far. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 20, 2007 Share Posted January 20, 2007 the variables in POST relate to the names of inputs, selects, and textareas. None of your form elements have those names. Quote Link to comment Share on other sites More sharing options...
peranha Posted January 20, 2007 Author Share Posted January 20, 2007 If I cant use the POST statement in the insert statement, why does it work in the Print_r statement. ??? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 20, 2007 Share Posted January 20, 2007 $_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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 20, 2007 Share Posted January 20, 2007 [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. Quote Link to comment 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.