Form for entering data into a MySQL table in PHP

The question is: what should be in the for loop for departments to be written to the drop-down list?

The MySQL database has two tables prepods and cafedrs. The prepods table consists of two fields - prepod and cafedra. cafedra is also in the cafedrs table, which is already filled in. The tables are linked.

You need to create an input form in the prepods table in PHP, so that the department field in the table is a drop-down list and all the values of this field are taken from the cafedrs table (cafedra field).

Php code

<?php
if ($_POST["submit"]) {
    if (!($connection=mysql_connect ("localhost", "root", "2357610"))) die ("Нет соединения");
    if (!(mysql_select_db ("imc", $connection))) die ("База ИМЦ не выбрана");
    $prepod=$_POST['prepod'];
    $cafedra=$_POST['cafedra'];
    if (!($cafedra=mysql_query ("SELECT cafedra from cafedrs"))) die ("Ошибка");
    $count=mysql_num_fields ($cafedra);
    while ($row=mysql_fetch_array ($cafedra)) {
        for($x=0;$x<$count;$x++) {//здесь нужно что-то сделать}
    }
    if (($cafedra!="") or ($prepod!="")) {
        $sql=mysql_query("INSERT INTO prepods VALUES (null,'$prepod','$row[$x]',1)");
        $result=mysql_query($sql);
        echo "<script type=\"text/javascript\"> alert(\"Данные добавлены\");</script> \n";
    } else {
        echo "<script type=\"text/javascript\"> alert(\"Заполните поля\");</script> \n";
    };
}
?>

The form itself:

<form action="pre.php" method="POST" name="form1">
    <table width="100%" align="center" valign=center>
        <tr>
            <td align=right>ФИО преподавателя:</td>
            <td>
                <input type="text" name="prepod">
                <br>
            </td>
        </tr>
        <tr>
            <td align=right>Наименование кафедры:</td>
            <td>
                <select name="row"></select>
                <br>
            </td>
        </tr>
        <tr>
            <td align=right>
                <br>
                <input type="submit" name="submit" value="Сохранить">
            </td>
        </tr>
    </table>
</form>
 0
Author: Nicolas Chabanovsky, 2012-04-30

1 answers

    <form action="pre.php" method="POST" name="form1">
    <table width="100%" align="center" valign=center>
        <tr>
            <td align=right>ФИО преподавателя:</td>
            <td>
                <input type="text" name="prepod">
                <br>
            </td>
        </tr>
        <tr>
            <td align=right>Наименование кафедры:</td>
            <td>
                <select name="row">
                 <?
if (!($cafedra=mysql_query ("SELECT cafedra from cafedrs"))) die ("Ошибка");
    $count=mysql_num_fields ($cafedra);
    while ($row=mysql_fetch_array ($cafedra)) {
        for($x=0;$x<$count;$x++) {
         echo $row["cafedra"];
}
    }
?>

                       </select>
                <br>
            </td>
        </tr>
        <tr>
            <td align=right>
                <br>
                <input type="submit" name="submit" value="Сохранить">
            </td>
        </tr>
    </table>
</form>
 1
Author: ilyosbey, 2012-05-01 12:15:24