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>