gesseg Posted June 16, 2010 Share Posted June 16, 2010 heres my code not sure why it doesnt work... <form action="insert.php" method="post"> name: <?php $con = mysql_connect("xxx","xxx","xxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxx", $con); $query="SELECT name FROM band_data2"; /* 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='name'> Name</option>"; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value=$nt[id]>$nt[name]</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> <br /> skill : <select name="skill"> <option value="">Do they hold their instuments up the right way?</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option></select> <input type="hidden" name="counter" value="1"> <input type="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/ Share on other sites More sharing options...
bugcoder Posted June 16, 2010 Share Posted June 16, 2010 try to change the name of drop down name from "name" to something else, works? Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072864 Share on other sites More sharing options...
gesseg Posted June 16, 2010 Author Share Posted June 16, 2010 Doesnt it need to be"name" because thats where its getting its data from? i.e. field "name" in database "band_data2". I tried it anyway but it didnt help. Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072870 Share on other sites More sharing options...
developerdave Posted June 16, 2010 Share Posted June 16, 2010 <form action="insert.php" method="post"> name: <?php $con = mysql_connect("xxx","xxx","xxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxx", $con); $query="SELECT name FROM band_data2"; /* 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="name"> Name</option>'; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value=\"{$nt['id']}\">{$nt['name']}</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> <br /> skill : <select name="skill"> <option value="">Do they hold their instuments up the right way?</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option></select> <input type="hidden" name="counter" value="1"> <input type="submit" /> </form> Sorry, forum killed it before. I've tidied it up a bit, try wrapping variables contained in string with braces. I'm assuming you're running PHP5? Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072873 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2010 Share Posted June 16, 2010 Since you didn't tell us what was not working, we're guessing here. You are using the "id" from the database records, but you're not selecting it with your query. <?php mysql_select_db("xxx", $con); $query="SELECT name, id FROM band_data2"; $result = mysql_query ($query); echo '<select name="name"> Name</option>'; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value='{$nt['id']}'>{$nt['name']}</option>\n"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072912 Share on other sites More sharing options...
Pikachu2000 Posted June 16, 2010 Share Posted June 16, 2010 Didn't look at this too deeply, but this line jumps out at me. You're trying to close an <option> tag that hasn't yet been opened. echo '<select name="name"> Name</option>'; Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072920 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2010 Share Posted June 16, 2010 Oops, missed that one... Ken Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072921 Share on other sites More sharing options...
Pikachu2000 Posted June 16, 2010 Share Posted June 16, 2010 Hope that didn't sound like it was directed at you, kenrbnsn. It wasn't meant to be . . . Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072922 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2010 Share Posted June 16, 2010 I know. It was directed at the OP. Ken Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072927 Share on other sites More sharing options...
gesseg Posted June 16, 2010 Author Share Posted June 16, 2010 Thanks but even with your revisions its still not doing what I want. To be honest Ive only been learning for a week. I can follow most of what the script is doing but havent got a clue how to edit to do what I want. What I want is a drop down list populated with my "name" field from my database, once the name is selected I want to pass the "name" data on to insert.php as $_POST data. I did have a text box that was working but in order to update the table you have to, obviously, type the name exactly. Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072929 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2010 Share Posted June 16, 2010 Ok. Please post you current code for both the form and insert.php What is happening now and why is it different from what you want? Have you looked at the generated source to make sure that the generated HTML is correct? Ken Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072932 Share on other sites More sharing options...
Pikachu2000 Posted June 16, 2010 Share Posted June 16, 2010 It also wouldn't hurt to remove the 'Name' string from within the <select></select> tag's structure . . . echo 'Name:<select name="name">'; // instead of // echo "<select name='name'> Name</option>"; Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072940 Share on other sites More sharing options...
developerdave Posted June 16, 2010 Share Posted June 16, 2010 Oops, missed that one... Ken Me too Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072941 Share on other sites More sharing options...
gesseg Posted June 16, 2010 Author Share Posted June 16, 2010 Heres vote.php. The code is taken from a tutorial on the web, <form action="insert.php" method="post"> name: <?php $con = mysql_connect("xxx","xxx","xxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("a3362005_bands", $con); $query="SELECT name FROM band_data2"; /* 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 'Name:<select name="name">'; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value=\"{$nt['id']}\">{$nt['name']}</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> <br /> skill : <select name="skill"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option></select> <input type="hidden" name="counter" value="1"> <input type="submit" /> </form></center> Heres the insert.php <?php $con = mysql_connect("xxx","xxx","xxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("a3362005_bands", $con); //Add to the table value with form data. mysql_query("UPDATE band_data2 SET skill = (skill + {$_POST['skill']}) WHERE name='{$_POST['name']}'"); mysql_query("UPDATE band_data2 SET skill_ave = (skill / counter) WHERE name='{$_POST['name']}'"); echo 'vote counted'; mysql_close($con); print_r ( $_POST ); ?> And here is the output from "print_r ( $_POST );" "Array ( [name] => [skill] => 10 [counter] => 1 )" i was told to put this in so i could see what was going on. As you can see the "name" from the dropdown isnt making it to the insert.php. Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072957 Share on other sites More sharing options...
developerdave Posted June 16, 2010 Share Posted June 16, 2010 Since you didn't tell us what was not working, we're guessing here. You are using the "id" from the database records, but you're not selecting it with your query. <?php mysql_select_db("xxx", $con); $query="SELECT name, id FROM band_data2"; $result = mysql_query ($query); echo '<select name="name"> Name</option>'; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value='{$nt['id']}'>{$nt['name']}</option>\n"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> Ken He has a point, I can't see this amendment in your revised code, perhaps try this? just after the mysql query try echo mysql_error(); And see if there's an error preventing the values being returned to the select field if it has no output, chances are it has no value. Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072968 Share on other sites More sharing options...
gesseg Posted June 16, 2010 Author Share Posted June 16, 2010 Fixed it sort of. Someone else on another forum had exactly the same problem so copied there revised code. Its still not working properly. It will only return the first word in the "name" field. As above but: while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value=$nt[name]>name: $nt[name]}</option>"; Now print_r ( $_POST ); gives: Array ( [name] => Coheed [skill] => 10 [counter] => 1 ) I want it to read: ( [name] => Coheed and Cambria [skill] => 10 [counter] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072974 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2010 Share Posted June 16, 2010 Since you aren't using the "id" field in your query, change your code for vote.php to <form action="insert.php" method="post"> <?php $con = mysql_connect("xxx","xxx","xxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("a3362005_bands", $con); $query="SELECT name FROM band_data2"; /* 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 'Name:<select name="name">'; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value=\"{$nt['name']}\">{$nt['name']}</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> <br /> skill : <select name="skill"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option></select> <input type="hidden" name="counter" value="1"> <input type="submit" /> </form></center> Also change the code for insert.php to <?php echo '<pre>' . print_r($_POST,true) . '</pre>'; $con = mysql_connect("xxx","xxx","xxx") or die("Could not connect<br>" . mysql_error()); mysql_select_db("a3362005_bands", $con); //Add to the table value with form data. $query = "UPDATE band_data2 SET skill = (skill + {$_POST['skill']}), skill_ave = (skill / counter) WHERE name='{$_POST['name']}'"; $rs = mysql_query($query) or die("Problem with the update query: $query<br>" . mysql_error()); echo 'vote counted'; ?> You only need one update query. Note: the reason you're new code is only getting one name is that the value is not enclosed in quotes. If you had looked at the generated HTML this would have been apparent. <?php echo "<option value=$nt[name]>name: $nt[name]}</option>"; ?> See my version above. Ken Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072976 Share on other sites More sharing options...
gesseg Posted June 16, 2010 Author Share Posted June 16, 2010 Thats great! Its working now thanks. I just wish I had a better grasp of why it is now working Dont suppose you want to point me in the right direction to getting the drop down in alphebetical order? Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072979 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2010 Share Posted June 16, 2010 To get the list sorted, change the query to <?php $query="SELECT name FROM band_data2 ASC"; ?> "ASC" means sort in ascending order, to do a reverse sort use "DESC". Ken Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1072990 Share on other sites More sharing options...
gesseg Posted June 16, 2010 Author Share Posted June 16, 2010 Thanks mate. Quote Link to comment https://forums.phpfreaks.com/topic/204928-mysql-populated-drop-down-not-_post-ing-data/#findComment-1073005 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.