I have a first name (fname) and last name (lname) from table_A displayed from a drop down list on a PHP page. I want the user to select a name (fname and lname together), check other radio buttons, and submit. The submited information will populate table_B, including fname and lname. Each table is in its own database and has a different amount of field names. I have been able to populate table_B with fname OR lname from table_A but not both. I am using an ODBC connection with MS SQL database.
My query to populate table_B:
<php>
<?php
$query="INSERT INTO table_B (fname, lname, other_fields) VALUES ('$fname', '$lname', '$other_fields');
?>
</php>
My script to select the first and last names. This code only inputs the fname into table_B. I want it to input fname AND lname.
<php>
<?php
$sql="SELECT lname, fname, group FROM table_A WHERE group='01' ORDER BY fname, lname";
echo "<select name = fname>";
$rs=odbc_exec($conn2, $sql);
while ($row = odbc_fetch_row ($rs))
{
echo "<option value=".odbc_result($rs, "fname").">".odbc_result($rs, 'fname' )." ".odbc_result($rs, 'lname' )."</option>";
}
echo "</select>";?>
?>
</php>
I want to <select name = fname> two values but only know how to select one value. Code not important to the question is left out. Thank you in advance for your help.

    Use php tags when posting php code, code tags or html tags as appropriate for other code.

    Validate and fix your html

    <select name = fname>

    should be

    <select name="fname">
    <!-- or -->
    <select name='fname'>

    Also, there is no reason to duplicate data such as fname and lname in other tables.

    CREATE TABLE person(
    	id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    	fname VARCHAR(50),
    	lname VARCHAR(50)
    );
    CREATE TABLE table_b (
    	id	INT UNSIGNED AUTO_INCREMENT PRIMARY KEY
    	person_id	INT UNSIGNED,
    FOREIGN KEY(person_id) REFERENCES person(id)
    );
    
    SELECT fname, lname
    FROM table_b
    INNER JOIN person ON person_id = person.id;
    

    Would shoud first and last name for any person that has a corresponding entry in table_b. This is called database normalization

    Next up, you can put whatever values you want in the option elements, and if the value and display should be the same, you don't even have to specify the value

    echo '<option>'.odbc_result($rs, 'fname').' '.odbc_result($rs, 'lname').'</option>';

    But since you should keep your db normalized, you do want another value than what is displayed

    echo '<option value="'.odbc_result($rs, 'id').'">'.odbc_result($rs, 'fname').' '.odbc_result($rs, 'lname').'</option>';

      Thank you for your reply. Table A is a view that holds many columns of information about the fname and lname (the employee). I can't change this database and table.
      Table B holds different information about fname and lname (the employee) which is selected by the user and viewed by all on a web page. The WHERE statement includes only current employees from Table A. Table B's information can be updated and deleted by a sub-admin. The fname and lname's are populated by a drop down from Table A and when submitted is matched with new information that was selected by the user in Table B. I choose a drop down to protect data integrity.
      Are you saying that the fname and lname should always come from Table A even though it is matched with new information in Table B? Then Table B would match an ID from Table A instead of a fname and lname with table joins for normalization?
      Summary: The user has to select his/her employee name from Table A by a drop down on a web page to be added with new information by a submit button to Table B and this information with the employee name is displayed in a new web page.

        Write a Reply...