mysql从下拉列表中选择

时间:2013-09-28 03:31:01

标签: php mysql

我有这个代码在文件中运行良好添加医生(add_doctor.php):

    <form enctype="multipart/form-data" form action="add_doctor.php" method="post">
    <fieldset>

<p><font color="green"><b>Studii și Specializări:</b></font></p>
<p><b>Universitatea:</b> 
    <select name="universit"><option>Selectează</option>
    <?php // Retrieve all the university and add to the pull-down menu.
    $q = "SELECT adm_id, CONCAT_WS(' ', universitate) FROM adm ORDER BY universitate ASC";      
    $r = mysqli_query ($dbc, $q);
    if (mysqli_num_rows($r) > 0) {
        while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
            echo "<option value=\"$row[0]\"";
            // Check for stickyness:
            if (isset($_POST['universit']) && ($_POST['universit'] == $row[0]) ) echo ' selected="selected"';
            echo ">$row[1]</option>\n";
        }
    } else {
        echo '<option>Vă rugăm adăugați o Universitate prima dată.</option>';
    }
    mysqli_close($dbc); // Close the database connection.
    ?>
    </select>&nbsp;&nbsp;&nbsp;<a href="adm_universitate.php" target=_"blank">Adaugă</a><font color="red">&nbsp;*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

但是在文件编辑医生(edit_doctor.php)我如何插入代码从下拉列表中的mysql表中选择大学?在我的代码中,我可以查看来自universitate字段的数据,但不能更改它。如何添加下拉列表,如上面的示例选择其他值?

// Retrieve the doctor's information:
$q = "SELECT universitate, numele, initiala_tatalui, prenumele, numele_anterior, sex, cnp, adresa_email, specialitate, specialitate2, an_abs_univ, seria_dipl_univ, nr_dipl_univ, first_name, user_id, data_adaugarii, first_name_mod, user_id_mod, stare, cod_parafa, data_modificarii FROM adm, medici, adm1 WHERE adm.adm_id=medici.adm_id AND adm1.adm1_id=medici.adm1_id AND medic_id=$id";        
$r = @mysqli_query ($dbc, $q);

if (mysqli_num_rows($r) == 1) { // Valid user ID, show the form.

// Get the doctor's information:
$row = mysqli_fetch_array ($r, MYSQLI_NUM);

// Create the form:

echo '<form action="edit_doctor.php" method="post">
<fieldset>
<p><font color="green"><b>Studii și Specializări:</b></font></p>

<p>Universitatea: <input type="text" readonly name="universit" size="50" maxlength="50" value="' . $row[0] . '" />&nbsp;&nbsp;&nbsp;
...
</fieldset>
<p></p>
<p><div align="center"><input type="submit" name="submit" value="Modifică" /></p>
<input type="hidden" name="id" value="' . $id . '" />
</form>';

我尝试过这种情况: 但是我得到错误而且不起作用。

// Retrieve the doctor's information:
$q = "SELECT universitate, numele, initiala_tatalui, prenumele, numele_anterior, sex, cnp, adresa_email, specialitate, specialitate2, an_abs_univ, seria_dipl_univ, nr_dipl_univ, first_name, user_id, data_adaugarii, first_name_mod, user_id_mod, stare, cod_parafa, data_modificarii FROM adm, medici, adm1 WHERE adm.adm_id=medici.adm_id AND adm1.adm1_id=medici.adm1_id AND medic_id=$id";        
$r = @mysqli_query ($dbc, $q);

if (mysqli_num_rows($r) == 1) { // Valid user ID, show the form.

// Get the doctor's information:
$row = mysqli_fetch_array ($r, MYSQLI_NUM);

// Create the form:

echo '<form action="edit_doctor.php" method="post">
<fieldset>
<p><font color="green"><b>Studii și Specializări:</b></font></p>

<p><b>Universitatea:</b> 
<select name="universit"><option>Selectează</option>
<?php // Retrieve all the artists and add to the pull-down menu.
$q = "SELECT adm_id, CONCAT_WS(' ', universitate) FROM adm ORDER BY universitate ASC";      
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) > 0) {
    while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
        echo "<option value=\"$row[0]\"";
        // Check for stickyness:
        if (isset($_POST['universit']) && ($_POST['universit'] == $row[0]) ) echo ' selected="selected"';
        echo ">$row[1]</option>\n";
    }
} else {
    echo '<option>Vă rugăm adăugați o Universitate prima dată.</option>';
}
mysqli_close($dbc); // Close the database connection.
?>
...
</fieldset>
<p></p>
<p><div align="center"><input type="submit" name="submit" value="Modifică" /></p>
<input type="hidden" name="id" value="' . $id . '" />
</form>';

在edit_doctor.php我需要的是从数据库(基于ID医生)检索关于医生的所有信息,包括大学字段,我希望它从下拉列表中的add_doctor.php中以相同的方式更改(得到它)来自数据库)。 在add_doctor.php很简单,因为我不需要收集任何数据,只是我插入一个新的医生。

感谢您的帮助。

好的,从三周开始我非常努力地检索医生信息,并在我的数据库中修改。有了你的帮助,我没有成功。也许这是简单的错误语法,所以我决定把这个来自edit_doctor.php的整个代码放在罗马尼亚语中,名为edit_medic.php。这段代码正在运行,但我无法通过Specialitate和Universitate(英语专业和大学)的下拉菜单改变价值。我得到空白页,我不知道为什么。如果我想更改数据库中的当前值,请帮助添加下拉列表以选择新的大学/专业。 这是我的代码:

<?php //edit_doctor.php en edit_medic.php ro
// This page is for editing a doctor record.
// This page is accessed through list_doctor.php.
//header('Content-type: text/html; charset=utf-8');
//header('Content-Type: text/html; charset=widows-cp1250' );
$page_title = 'Editează Medic';
include ('includes/header.html');

echo '<h1>Detalii Medic';
if (isset($_SESSION['first_name'])) {
echo ", {$_SESSION['first_name']}";
}
if (isset($_SESSION['user_id'])) {
echo ", User ID: {$_SESSION['user_id']}";

}
echo '!</h1>';

//aici e 1 cu cel de jos, adika daca nu e logat nu are acces la baza de date
// Display links based upon the login status:
if (isset($_SESSION['user_id'])) {

// Check for a valid user ID, through GET or POST:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From list_doctor.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form submission.
$id = $_POST['id'];
} else { // No valid ID, kill the script.
echo '<p class="error">This page has been accessed in error1.</p>';
include ('includes/footer.html'); 
exit();
}

require ('mysqli_fa_conexiunea.php');

// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$errors = array();

// Check for a first name:
if (empty($_POST['numele'])) {
    $errors[] = 'Câmpul Numele este gol !';
} else {
    $num = mysqli_real_escape_string($dbc, trim($_POST['numele']));
}

    // Check for a first name:
if (empty($_POST['initiala_tatalui'])) {
    $errors[] = 'Câmpul Inițiala Tatălui este gol !';
} else {
    $ita = mysqli_real_escape_string($dbc, trim($_POST['initiala_tatalui']));
}

// Check for a last name:
if (empty($_POST['prenumele'])) {
    $errors[] = 'Câmpul Prenumele este gol !';
} else {
    $pre = mysqli_real_escape_string($dbc, trim($_POST['prenumele']));
}

    // Check for a previous name:
//if (empty($_POST['numele_anterior'])) {
//  $errors[] = 'Câmpul Numele Anterior este gol !';
//} else {
    $nan = mysqli_real_escape_string($dbc, trim($_POST['numele_anterior']));
//}

    $sex = mysqli_real_escape_string($dbc, trim($_POST['sex']));

    // Check CNP:
if (empty($_POST['cnp'])) {
    $errors[] = 'Câmpul CNP este gol !';
} else {
    $cnp = mysqli_real_escape_string($dbc, trim($_POST['cnp']));
}

// Check for an email address:
if (empty($_POST['adresa_email'])) {
    $errors[] = 'Câmpul Adresa Email este gol !';
} else {
    $aem = mysqli_real_escape_string($dbc, trim($_POST['adresa_email']));
}

    // Check for a speciality2:
//if (empty($_POST['specialitate2'])) {
//  $errors[] = 'Ați uitat sa introduceți Specialitate 2.';
//} else {

    $speci2 = mysqli_real_escape_string($dbc, trim($_POST['specialitate2']));
            $an_abs_univ = mysqli_real_escape_string($dbc, trim($_POST['an_abs_univ']));
            $seria_dipl_univ = mysqli_real_escape_string($dbc, trim($_POST['seria_dipl_univ']));
            $nr_dipl_univ = mysqli_real_escape_string($dbc, trim($_POST['nr_dipl_univ']));
            $stare = mysqli_real_escape_string($dbc, trim($_POST['stare']));
            $cod_parafa = mysqli_real_escape_string($dbc, trim($_POST['cod_parafa']));
            $first_name = $_POST['first_name'];
            $user_id = $_POST['user_id'];
            $cm_nr = mysqli_real_escape_string($dbc, trim($_POST['cm_nr']));
            $cm_data_elib = mysqli_real_escape_string($dbc, trim($_POST['cm_data_elib']));
//}

    // Validate the university...
//if ( isset($_POST['universit']) && filter_var($_POST['universit'], FILTER_VALIDATE_INT, array('min_range' => 1))  ) {
//  $univ = $_POST['universit'];
//} else { // No artist selected.
//  $errors[] = 'Vă rugăm selectați Universitatea !';
//}

if (empty($errors)) { // If everything's OK.

    //  Test for unique email address:
    $q = "SELECT medic_id FROM medici WHERE adresa_email='$aem' AND medic_id != $id";
    $r = @mysqli_query($dbc, $q);
    if (mysqli_num_rows($r) == 0) {
            //  Test for unique CNP:
    $q = "SELECT medic_id FROM medici WHERE cnp='$cnp' AND medic_id != $id";
    $r = @mysqli_query($dbc, $q);
    if (mysqli_num_rows($r) == 0) {

        // Make the query:
        $q = "UPDATE medici SET numele='$num', initiala_tatalui='$ita', prenumele='$pre', numele_anterior='$nan', cnp='$cnp', adresa_email='$aem', stare='$stare', cod_parafa='$cod_parafa', specialitate2='$speci2', an_abs_univ='$an_abs_univ', seria_dipl_univ='$seria_dipl_univ', nr_dipl_univ='$nr_dipl_univ',  cm_nr='$cm_nr', cm_data_elib='$cm_data_elib', data_modificarii=NOW(), first_name='$first_name', user_id='$user_id', first_name_mod='{$_SESSION['first_name']}', user_id_mod='{$_SESSION['user_id']}' WHERE medic_id=$id LIMIT 1";//LIMIT 1
        $r = @mysqli_query ($dbc, $q);
        if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.

            // Print a message:
            echo '<p><font color="red"><b>Datele Medicului au fost modificate.</b></font></p>'; 

        } else { // If it did not run OK.
            echo '<p class="error">Medicul nu a fost modificat ca urmare a unei erori de sistem.</p>'; // Public message.
            echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message.
        }
    } else { // Already registered.
        echo '<p class="error">CNP a fost deja înregistrat.</p>';
    }       
    } else { // Already registered.
        echo '<p class="error">Adresa de email a fost deja înregistrată.</p>';
    }

} else { // Report the errors.

    echo '<p class="error">The following error(s) occurred:<br />';
    foreach ($errors as $msg) { // Print each error.
        echo " - $msg<br />\n";
    }
    echo '</p><p>Vă rugăm încercați din nou.</p>';

} // End of if (empty($errors)) IF.

} // End of submit conditional.

// Always show the form...

// Retrieve the doctor's information:
$q = "SELECT universitate, numele, initiala_tatalui, prenumele, numele_anterior, sex, cnp, adresa_email, specialitate, specialitate2, an_abs_univ, seria_dipl_univ, nr_dipl_univ, first_name, user_id, data_adaugarii, first_name_mod, user_id_mod, stare, cod_parafa, cm_nr, cm_data_elib, data_modificarii FROM adm, medici, adm1 WHERE adm.adm_id=medici.adm_id AND adm1.adm1_id=medici.adm1_id AND medic_id=$id";       
$r = @mysqli_query ($dbc, $q);

if (mysqli_num_rows($r) == 1) { // Valid user ID, show the form.

// Get the user's information:
$row = mysqli_fetch_array ($r, MYSQLI_NUM);

// Create the form:

echo '<form action="edit_medic.php" method="post">
<fieldset>
<p><font color="green"><b>Informații Personale:</b></font></p>
<p>Numele: <input type="text" name="numele" size="15" maxlength="15" value="' . $row[1] . '" />&nbsp;&nbsp;&nbsp;
Initiala tatalui: <input type="text" name="initiala_tatalui" size="15" maxlength="15" value="' . $row[2] . '" />&nbsp;&nbsp;&nbsp;
Prenumele: <input type="text" name="prenumele" size="15" maxlength="30" value="' . $row[3] . '" /></p>
<p>Numele Anterior: <input type="text" name="numele_anterior" size="15" maxlength="15" value="' . $row[4] . '" /></p>
<p>SEX: <input type="text" readonly name="sex" size="1" maxlength="1" value="' . $row[5] . '" /></p>
<p>CNP: <input type="text" name="cnp" size="15" maxlength="15" value="' . $row[6] . '" /></p>
<p>Adresa Email: <input type="text" name="adresa_email" size="50" maxlength="50" value="' . $row[7] . '"  /> </p>

<hr>
<p><font color="green"><b>Informații Medic:</b></font></p>
<p>Stare: <input type="text" readonly name="stare" size="25" maxlength="25" value="' . $row[18] . '"  /> 
&nbsp;Modifică în:<input type="radio" name="stare" value="Public" />Public <input type="radio" name="stare" value="Privat" />Privat <input type="radio" name="stare" value="Public + Privat" />Public + Privat <input type="radio" name="stare" value="Nu lucrează" />Nu lucrează <input type="radio" name="stare" value="Transferat RO" />Transferat RO <input type="radio" name="stare" value="Transferat Străinatate" />Transferat Străinătate <input type="radio" name="stare" value="Pensionar" />Pensionar <input type="radio" name="stare" value="Pensionar Public" />Pensionar Public <input type="radio" name="stare" value="Pensionar Privat" />Pensionar Privat <input type="radio" name="stare" value="Decedat" />Decedat</p>
<p>Cod Parafă: <input type="text" name="cod_parafa" size="10" maxlength="10" value="' . $row[19] . '" /></p>

<hr>
<p><font color="green"><b>Studii și Specializări:</b></font></p>

<p>Universitatea: <input type="text" readonly name="universit" size="50" maxlength="50" value="' . $row[0] . '" />&nbsp;&nbsp;&nbsp;
An Absolvire: <input type="text" name="an_abs_univ" size="4" maxlength="4" value="' . $row[10] . '"  />&nbsp;&nbsp;&nbsp;
Serie Diplomă: <input type="text" name="seria_dipl_univ" size="3" maxlength="3" value="' . $row[11] . '"  />&nbsp;&nbsp;&nbsp;
Nr. Diplomă: <input type="text" name="nr_dipl_univ" size="10" maxlength="10" value="' . $row[12] . '"  /></p>

<p>Specialitate 1: <input type="text" readonly name="specialitate" size="50" maxlength="50" value="' . $row[8] . '"  />
&nbsp;Modifică în:      
 <input type="text" name="specialitate" size="50" maxlength="50" value="' . $row[8] . '"  />

 </p>
<p>Specialitate 2: <input type="text" name="specialitate2" size="50" maxlength="50" value="' . $row[9] . '"  /></p>

<hr>
<p><font color="green"><b>Certificat de Membru</b></font></p>
<p><b>Nr. Certificat:</b> <input type="text" name="cm_nr" size="5" maxlength="5" value="' . $row[20] . '"  />
<b>Data Eliberării:</b>&nbsp; <input type="text" name="cm_data_elib" size="10" maxlength="10" value="' . $row[21] . '"  />
<hr>
<p><font color="green"><b>Evidență Adăugiri și Modificări:</b></font></p>
<p>Adăugat de: <input type="text" readonly name="first_name" size="50" maxlength="50" value="' . $row[13] . '"  />
User ID: <input type="text" readonly name="user_id" size="5" maxlength="5" value="' . $row[14] . '"  />
La data de: <input type="text" readonly name="data_adaugarii" size="15" maxlength="15" value="' . $row[15] . '"  />
<p>Modificat de: <input type="text" readonly name="first_name_mod" size="50" maxlength="50" value="' . $row[16] . '"  />
User ID: <input type="text" readonly name="user_id_mod" size="5" maxlength="5" value="' . $row[17] . '"  />
La data de: <input type="text" readonly name="data_modificarii" size="15" maxlength="15" value="' . $row[22] . '"  />
<p>Acum modifică: <input type="text" readonly name="first_name_mod" size="50" maxlength="50" value="'.$_SESSION['first_name'].'"  />
User ID: <input type="text" readonly name="user_id_mod" size="5" maxlength="5" value="'.$_SESSION['user_id'].'"  /></p>


</fieldset>
<p></p>
<p><div align="center"><input type="submit" name="submit" value="Modifică" /></p>
<input type="hidden" name="id" value="' . $id . '" />
</form>';
    echo '<form method="post" action="lista_medici.php">
<input type="submit" value="Înapoi"></div>
</form>';

} else { // Not a valid user ID.
echo '<p class="error">This page has been accessed in error2.</p>';
}

mysqli_close($dbc);

//aici e 2 cu cel de sus, adika daca nu e logat nu are acces la baza de date
} else { //  Not logged in.
    echo '<br><a href="register.php" title="Înregistrare nouă">Înregistrare nouă</a><br />
<a href="login.php" title="Autentificare">Autentificare</a><br />
<a href="forgot_password.php" title="Recuperare Parolă">Recuperare Parolă</a><br />
';
}

include ('includes/footer.html');
?>

4 个答案:

答案 0 :(得分:0)

Hi You try this code..
I can't understand what you want? You have any queries tell me.
// Retrieve the doctor's information:
<form action="edit_doctor.php" method="post">
<fieldset>
<p><font color="green"><b>Studii și Specializări:</b></font></p>

<?php 
$q = "SELECT universitate, numele, initiala_tatalui, prenumele, numele_anterior, sex, cnp, adresa_email, specialitate, specialitate2, an_abs_univ, seria_dipl_univ, nr_dipl_univ, first_name, user_id, data_adaugarii, first_name_mod, user_id_mod, stare, cod_parafa, data_modificarii FROM adm, medici, adm1 WHERE adm.adm_id=medici.adm_id AND adm1.adm1_id=medici.adm1_id AND medic_id=$id";        
$r = @mysqli_query ($dbc, $q);

if (mysqli_num_rows($r) == 1) { // Valid user ID, show the form.

// Get the doctor's information:
while($row = mysqli_fetch_array ($r, MYSQLI_NUM))
{?>

// Create the form:
<input type="hidden" name="id" value="<?php echo $id;?>" />
<p>Universitatea: <input type="text" readonly name="universit" size="50" maxlength="50" value="<?php echo $row[0]; ?>" />

<?php }}?>

</fieldset>
<p></p>
<p><div align="center"><input type="submit" name="submit" value="Modifică" /></p>

</form>

答案 1 :(得分:0)

似乎您的问题与数据库或下拉菜单无关,只是基本的PHP语法。

有多种方法可以从PHP输出HTML;简化一点:

  • 输出<?php ... ?>包装器外的任何文本。因此,在您的第一个文件中,您有?><form ...>输出表单,当您需要更多PHP时,可以使用<?php重新打开。
  • echo语句输出字符串epxression - 变量,函数或带引号的字符串。因此,在第二个文件中,您有echo '<form...,字符串会一直持续到匹配的';标记echo的结尾。

在您尝试过的代码中,您混合了两个:您仍然在以'开头的字符串中,但您已粘贴<?php。这不起作用。

您需要做的只有多个echo,因此您可以结束echo '...';,然后重新开始:

echo '<form action="edit_doctor.php" method="post">
... 
<select name="universit"><option>Selectează</option>';
// Retrieve all the artists and add to the pull-down menu.
$q = "SELECT adm_id, CONCAT_WS(' ', universitate) FROM adm ORDER BY universitate ASC";      
$r = mysqli_query ($dbc, $q);
// ...
mysqli_close($dbc); // Close the database connection.
echo '
...
</fieldset>
...
</form>';

答案 2 :(得分:0)

您可以尝试使用相同的表格代码。

if (mysqli_num_rows($r) == 1) 
{
    $action = "edit_doctor.php";
}
else
{
    $action = "add_doctor.php";
}

echo '<form action="$action" method="post">';

答案 3 :(得分:0)

<?php
// Retrieve the doctor's information:
$q = "SELECT universitate, numele, initiala_tatalui, prenumele, numele_anterior, sex, cnp, adresa_email, specialitate, specialitate2, an_abs_univ, seria_dipl_univ, nr_dipl_univ, first_name, user_id, data_adaugarii, first_name_mod, user_id_mod, stare, cod_parafa, data_modificarii FROM adm, medici, adm1 WHERE adm.adm_id=medici.adm_id AND adm1.adm1_id=medici.adm1_id AND medic_id='".$id."'";        
$r = @mysqli_query ($dbc, $q);

if (mysqli_num_rows($r) == 1) { // Valid user ID, show the form.

// Get the doctor's information:
$row = mysqli_fetch_array ($r, MYSQLI_NUM);

// Create the form:
?>
<form action="edit_doctor.php" method="post">
<fieldset>
<p><font color="green"><b>Studii și Specializări:</b></font></p> 

<p><b>Universitatea:</b> 
<select name="universit"><option>Selectează</option>
<?php // Retrieve all the artists and add to the pull-down menu.
$q = "SELECT adm_id, CONCAT_WS(' ', universitate) FROM adm ORDER BY universitate ASC";      
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) > 0) {
    while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
        ?>
   <option value="<?php echo $row[0];?>" <?php if (isset($_POST['universit']) && ($_POST['universit'] == $row[0]) ){ echo ' selected="selected"'; }?>>
   <?php echo $row[1];?>
   </option>
   <?php }
} else {
    echo '<option>Vă rugăm adăugați o Universitate prima dată.</option>';
}
mysqli_close($dbc); // Close the database connection.
?>
...
</fieldset>
<p></p>
<p><div align="center"><input type="submit" name="submit" value="Modifică" /></p>
<input type="hidden" name="id" value="<?php echo $id;?>" />
</form>