0

Need Help guys...

Ive created a form where a user can input First Names and Last Names then sent to database.

Then,

Ive created a dynamic drop down list where the user can select items such as job position or year enrolled. The selected option will be stored to a different table together with the user's first name

and last name.

Although I was able to populate the drop down list from my database. How can I fetch the value

from the drop down list and insert it together with the submitted form to the different table.

Here's my code. Please help... tnx

<html>
<head>
<title>NEW EMPLOYEE</title>

<?php

    mysql_connect("localhost","root","");
    $db1 = "emp_db1";
    $t3 ="emp_table";
    mysql_query("create database $db1");
    mysql_select_db($db1);
    mysql_query("create table $t3(id int not null auto_increment primary key,fname varchar(50),lname varchar(50),mname varchar(50),post_name varchar(50)) engine = InnoDB");

?>

</head>

<BODY bgcolor ="pink">



<?php

    if(isset($_POST['submit'])){
    $fname =$_POST['fname'];
    $lname =$_POST['lname'];
    $mname =$_POST['mname'];
    $post_name =$_POST['post_name'];
    $submit=$_POST['submit'];

    error_reporting(E_ALL ^ E_NOTICE);

    mysql_query("INSERT INTO emp_table(fname,lname,mname,post_name) VALUES('$fname','$lname','$mname','$post_name')");

    }

?>



<form name="add_data" method="post" target="<?php $_SERVER['PHP_SELF']?>">

    Register New Employee <br/><br/>
    First Name:<input size = "20" type="text" name="fname"/>
    Middle Name:<input size = "20" type="text" name="mname"/>
    Last Name:<input size = "20" type="text" name="lname"/><br/>
    Job Position List:
        <select name='post_name'>
        <option value="">--- Select ---</option>
        <?php
            if (isset ($db1)&&$db1!=""){

            }
        ?>
        <?php
            $list=mysql_query("select * from post_table order by id asc");

            //Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\testA\index.php on line 56
            while($row_list=mysql_fetch_assoc($list)){
        ?>
                <option value="<?php echo $row_list['id']; ?>"<?php if($row_list['id']==$db1){echo "selected"; } ?>>
                                     <?php echo $row_list['post_name'];?>
                </option>
            <?php
            }
            ?>
        </select>

    <input type="submit" name="submit" value="REGISTER" />

   </form>
</body>

raziel2101
  • 13
  • 3
  • 8

1 Answers1

0

There are many questions and issues for just one post :)

  1. It does not look like a good idea that you create a new table at the beginning of the script.

  2. The mysql_* functions are deprecated. See the warning at: https://www.php.net/manual/en/function.mysql-query.php

  3. You do not need the <form target="..."> attribute, if you want the form to post back to the same URL.

  4. For the actual question: It looks like you have all code in place actually. It just needs some improvements. Is there anything specific that does not work for you?

What should be done:

  • Redirect to the "success" page after you insert data. Unless you want to stay on submission path to allow another insert. But you should at least insert a paragraph with "success" message (e.g. above the form).
  • Validation of the fields.
  • Some error handling.

See also questions:

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Well you are correct in referring to creating table at the beginning, im planning to remove it for finalization. Well for the drop down list, I manage to make it work somehow but the problem is instead of inserting the position name to the emp_table, the post_id was inserted instead. How do I insert the post_name from the post_table like "Computer Programmer" to the emp_table's post_name column?.Im still quite new to php. hehehe – raziel2101 Apr 17 '13 at 06:08
  • Just use position name for `value` attribute of `option` tag: ` – Martin Prikryl Apr 17 '13 at 06:34