I have two tables, user_info with fields: 'name', 'userid', 'email', 'phone', 'pass' ,'role_id', and user_role with fields: 'role_id' and 'role_name'. I have created two rows in the second table, first, role_id: 1, role_name: Owner and second, role_id: 2, role_name: Customer. I want my dropdown to get values from there and I tried doing this but it didn't work. The first block is code is form validation and inserting data into the database(this works) and the second is trying to get dynamic values in select option.
<-- form -->
<?php
include('db_conn.php');
if(isset($_POST["submitform"])){
$name = mysqli_real_escape_string($con, $_POST['name']);
$userid = mysqli_real_escape_string($con, $_POST['userid']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$pass = md5($_POST['pass']);
$cpass = md5($_POST['cpass']);
$role_id = $_POST['role_id'];
$select = "SELECT * FROM user_info WHERE email = '$email'
&& pass = '$pass' ";
$result = mysqli_query($con, $select);
if(mysqli_num_rows($result) > 0){
$error[] = 'user already exists';
}else{
if($pass != $cpass){
$error[]= 'password does not match';
}else{
$insert = "INSERT INTO user_info (name, userid, email, phone, pass, role_id)
VALUES ('$name', '$userid', '$email', '$phone', '$pass', '$role_id')";
mysqli_query($con, $insert);
header('location: login.php');
}
}
};
?>
<-- dropdown list -->
<select style="padding:5px; width:200px; border-radius:20px;">
<option>select role</option>
<?php
include('db_conn.php');
$query = mysqli_query("SELECT * FROM user_info");
$user_roles = mysqli_query ($con, $sql);
$count = mysqli_num_rows($user_roles);
?>
<?php
for($i=1; $i<=$count; $i++){
$row = mysqli_fetch_array($user_roles)
?>
<option value="<?php echo $row["role_id"]; ?>">
<?php echo $row["role_name"]; ?>
</option>
<?php
}
?>
</select>