Jump to content

Putting dynamic menu results back into database


davidcriniti

Recommended Posts

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***********************

?>

 

 

Link to comment
Share on other sites

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

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.