0

I am creating a form using php where a user can select an option from the drop-down list and store in another table of the same database after submit.

i created two tables of the same database(db) one is a user table and another is a administrator table lets say i want the dropdown to list all the names stored in user table using the id of user names, how do i fetch the data from the user table and display it in the drop down list and upon selection of a name from the drop down it should store in administrator table after i click the submit.

and also how do i validate if adding a same user

this is the code below i am trying to get it work

<?php
if(isset($_POST["submit"])){
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "db";
    try {
        $dbh = new PDO("mysql:host=$servername;dbname=$db",$username,$password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "SELECT name FROM `users`";
        $result = mysql_query($sql);
        if ($dbh->query($sql)) {
            echo "<script type= 'text/javascript'>alert('New Record Inserted  Successfully');</script>";
        }
        else{
            echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
        }

        $dbh = null;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
}
?>
<div class="container">
    <h1>Admin</h1>
    <form name="reg" action="" method="post">
        <table class="table-borderless">
            <tr>
                <td>admin1</td>
                <td>
                    <select>
                        <?php while($row = mysql_fetch_array($result)):;?>
                            <option value="<?php echo $row[0];?>"><?php echo $row[1];?></option>
                        <?php endwhile;?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>admin2</td>
                <td>
                    <select>
                        <?php while($row = mysql_fetch_array($result)):;?>
                            <option value="<?php echo $row1[0];?>"><?php echo $row[1];?></option>
                        <?php endwhile;?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>admin3</td>
                <td>
                    <select>
                        <?php while($row = mysql_fetch_array($result)):;?>
                            <option value="<?php echo $row[0];?>"><?php echo $row[1];?></option>
                        <?php endwhile;?>
                    </select>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" name="submit" value="Register" />
                    <input type="reset" name="cancel" value="clear"/>
                </td>
            </tr>
        </table>
    </form>
</div>
Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
Deep13
  • 55
  • 1
  • 12
  • Code indentation, make code easier to read, but **more importantly easier to DEBUG** – RiggsFolly Apr 21 '16 at 10:59
  • You cannot connect to the database using PDO and then query the database using `mysql_*` functions – RiggsFolly Apr 21 '16 at 11:01
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Apr 21 '16 at 11:02

0 Answers0