davidcriniti Posted June 28, 2011 Share Posted June 28, 2011 Hi, I have a form which a coach uses to upload programs (usually excel documents) for his athletes. This part of the form works perfectly. However, the coach doesn't type the athlete's name into a text field. Rather, the athlete's name is selected from a dropdown menu which is populated by the names of athletes in the database. See code below: <?php include 'mysql_connect.php'; $result = mysql_query( "SELECT firstname, lastname, athlete_id FROM t_athletes ORDER BY lastname ASC" ) or die("SELECT Error: ".mysql_error()); $options = "<option selected>Please select...</option>"; while ($row=mysql_fetch_array($result)) { $options .= "<option value=\"$row[id]\">{$row['firstname']}"." "."{$row['lastname']}</option>"; } ?> <label> <select name="id" id="id"> <?php echo $options?> </select> </label> The athlete's id is the only thing that doesn't go through to the database. Not sure if I need to change something in the code above or something in the form processing code below. Any ideas? <?php //MySQL Database Connect include 'mysql_connect.php'; global $_POST; $id = $_POST["id"] ; $prog_expiry_date = $_POST["prog_expiry_date"] ; $program_link = $_POST["program_link"] ; $program_message = $_POST["program_message"] ; $docx=$_FILES['program_link']['name']; if($docx!="") { if (!copy($_FILES['program_link']['tmp_name'], "athlete_programs/$docx")) { echo "failed to copy \n"; } } //**********************SEND TO DATABASE**************************** //MySQL Database Connect include 'mysql_connect.php'; $query = "INSERT INTO programs_for_athletes (date, athlete_id_prog, program_link, prog_expiry_date, program_message)" . "VALUES (NOW(), '$id', '$docx', '$prog_expiry_date' , '$program_message')"; //if($query){echo 'data has been placed'} mysql_query($query) or die(mysql_error()); //***********************END OF DATABASE CODE*********************** ?> Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 28, 2011 Share Posted June 28, 2011 It seems the value of ID is not reaching the next page. Are you sure your <select> box is inside the <form> ? Quote Link to comment Share on other sites More sharing options...
davidcriniti Posted June 28, 2011 Author Share Posted June 28, 2011 Yep, it was inside the form. I managed to get it working though it seems to be a case of more good luck than good management. Here's the working code: (Part 1 from the form): <?php include 'mysql_connect.php'; $result = mysql_query( "SELECT firstname, lastname, athlete_id FROM t_athletes ORDER BY lastname ASC" ) or die("SELECT Error: ".mysql_error()); $options = "<option selected>Please select...</option>"; while ($row=mysql_fetch_array($result)) { $options .= "<option value=\"$row[athlete_id]\">{$row['firstname']}"." "."{$row['lastname']}</option>"; } ?> <label> <select name="athlete_id" id="athlete_id"> <?php echo $options?> </select> </label> ...and the code that processes the form: <?php //MySQL Database Connect include 'mysql_connect.php'; global $_POST; $athlete_id = $_POST["athlete_id"] ; $prog_expiry_date = $_POST["prog_expiry_date"] ; $program_link = $_POST["program_link"] ; $program_message = $_POST["program_message"] ; $docx=$_FILES['program_link']['name']; if($docx!="") { if (!copy($_FILES['program_link']['tmp_name'], "athlete_programs/$docx")) { echo "failed to copy \n"; } } //**********************SEND TO DATABASE**************************** //MySQL Database Connect include 'mysql_connect.php'; $query = "INSERT INTO programs_for_athletes (date, athlete_id_prog, program_link, prog_expiry_date, program_message)" . "VALUES (NOW(), '$athlete_id', '$docx', '$prog_expiry_date' , '$program_message')"; //if($query){echo 'data has been placed'} mysql_query($query) or die(mysql_error()); //***********************END OF DATABASE CODE*********************** ?> Cheers, Dave Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 28, 2011 Share Posted June 28, 2011 so you just changed the <select> name to 'athlete_id' and it worked? that probably means you already have an element somewhere called 'id' that was conflicting with your <select> Quote Link to comment Share on other sites More sharing options...
davidcriniti Posted June 28, 2011 Author Share Posted June 28, 2011 Yeah that's pretty much all I did. Thanks for that tip - I'll watch out for the same thing in future. 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.