The teacher table has elements of teacher's name , position, userid and password. I want to replace the userid
with teacher's name in the another table named Subject
so that it is easier to refer. So how can I do it using PHP coding ?
<?php
include ('connection.php');
$data1 = mysqli_query($connect,"SELECT * FROM teacher");
while ($info1=mysqli_fetch_array($data1))
{
echo "<option hidden selected> -- Your Option -- </option>";
echo "<option value=$info1[userid]>$info1[name]</option>";
}
I expect to have the teacher's name as an option but it came out $info1[name]
You just need to do syntax changes as follows:
while ($info1=mysqli_fetch_array($data1))
{
echo "<option hidden selected> -- Your Option -- </option>";
echo "<option value='$info1[userid]'>'$info1[name]'</option>";
}
Because PHP take $info1[name] as a string, you need to use single quote to interpret the variable value.
I usually prefer a much clearer syntax, which helps me in checking I refer to all in a correct manner:
while ($info1=mysqli_fetch_array($data1))
{
echo "<option hidden selected> -- Your Option -- </option>";
echo "<option value=\"".$info1["userid"]."\">".$info1["name"]."</option>";
}
With that syntax you can always be sure, by simply looking at the code, that PHP will recognize and insert proper values...
Also, check that field names in the table do match exactly (even in uppercase/lowercase, not all db configurations are case insensitive) the names you use: "userid", "name", etc.