Php 将组合框中的选定值存储到数据库中

Php 将组合框中的选定值存储到数据库中,php,html,mysql,Php,Html,Mysql,我试图创建一个带有文本输入和组合框的表单,其中包含从数据库检索到的值和数据。其思想是,当您提交表单时,文本字段中的值将存储在数据库的一个字段中。我需要做的是将组合框(而不是组合框中显示的文本)的选定值(数字值和从db的其他表中提取的值)存储到将存储表单文本字段的同一个表中 数据库结构由两个名为countries和addressions的表组成。国家/地区有一个名为country_id的字段和另一个名为country_name的字段(这两个字段填充组合框。表格名称有三个名为Attachion_id

我试图创建一个带有文本输入和组合框的表单,其中包含从数据库检索到的值和数据。其思想是,当您提交表单时,文本字段中的值将存储在数据库的一个字段中。我需要做的是将组合框(而不是组合框中显示的文本)的选定值(数字值和从db的其他表中提取的值)存储到将存储表单文本字段的同一个表中

数据库结构由两个名为countries和addressions的表组成。国家/地区有一个名为country_id的字段和另一个名为country_name的字段(这两个字段填充组合框。表格名称有三个名为Attachion_id的字段(pk、nn、ai)、国家/地区id和称谓名称。称谓名称字段必须填写表单文本字段中提交的数据,国家/地区id必须填写组合框的选定值,该值等于国家/地区表的国家/地区id值。 到目前为止,我已经能够创建表单,并用db值加载combobox中的所有国家/地区

表格代码为

<form name="New Appellation" action="actions/register_appellation.php" method="POST"     onsubmit="return validate()">
Nombre denominacion de origen:
<input type="text" placeholder="Nombre DO" name="fappellation_name">
<br>
Pais denominacion de origen:
<?php
include_once 'includes/fast_conn.php';
mysqli_select_db($cn,$db_name) or die(mysql_error());
$sql = "SELECT country_name, country_id FROM countries";
$rs = mysqli_query($cn,$sql) or die(mysql_error());
echo "<select name='appellation_country'>";
while($row = mysqli_fetch_array($rs)){
echo "<option value='".$row["country_id"]."'>".$row["country_name"]."</option>";
}mysqli_free_result($rs);
echo "</select>";
?>
<input type="submit" value="Registrar">

</form>

原厂名称:

原发性血液透析:
我已经在下面列出了应该处理这个问题的修改后的来源

我在源代码中做了一些事情。我相信所有这些都应该被很好地记录下来。下面是我添加的事情列表:

  • 代码现在已更新为完全使用MySQLi
  • 两种形式的MySQLi都是OOP风格的(我更喜欢MySQLi)
  • 有错误检查,看看事情是否成功,并相应地显示错误
  • register\u addression.php
    现在检查字段是否已填写
  • register\u atternation.php
    现在对查询变量执行卫生处理。(有助于防止SQL注入攻击)
  • register\u atternation.php
    现在通过与
    countries
    表交叉引用来确认它收到的国家id是有效的

  • 以下是表格代码:

    <form name="New Appellation" action="actions/register_appellation.php" method="POST"    onsubmit="return validate()">
    Nombre denominacion de origen:
    <input type="text" placeholder="Nombre DO" name="fappellation_name">
    <br>
    Pais denominacion de origen:
    <?php
    include_once 'includes/fast_conn.php';
    $boolSelectionSuccessful = $cn->select_db($db_name);
    if(!$boolSelectionSuccessful){
        $deathMessage = "<br><div>" .
            "Oh no! An error occurred on our end!<br>" .
            "Here's what we know:<hr>" .
            $cn->error .
            "<hr>" .
            "If this error continues to occur, please contact the website administrator." .
            "</div>";
        die($deathMessage);
    }
    $sql = "SELECT `country_name`, `country_id` FROM `countries`";
    $rs = $cn->query($sql);
    if($rs === FALSE){
        $deathMessage = "<br><div>" .
            "Oh no! An error occurred on our end!<br>" .
            "Here's what we know:<hr>" .
            $cn->error .
            "<hr>" .
            "If this error continues to occur, please contact the website administrator." .
            "</div>";
        die($deathMessage);
    }
    echo "<select name='appellation_country'>";
    while($row = $rs->fetch_assoc()){
        echo "<option value='".$row["country_id"]."'>".$row["country_name"]."</option>";
    }
    $rs->close();
    echo "</select>";
    ?>
    <input type="submit" value="Registrar">
    </form>
    
    
    原厂名称:
    
    原发性血液透析:
    您正在将MySQL API与
    MySQL\u error()
    混合使用,它们不会混合使用。使用错误报告会发出这样的信号。将MySQL\u error()更改为mysqli\u error($cn),但没有更改。仍然得到通知:未定义索引:register_addression.php第4行中的country_id这是因为您的表单没有名为
    country_id
    的命名元素来处理
    $fccountry_id=$\u POST['country_id'];
    。其中唯一的元素是
    ,这就是为什么会出现未定义索引警告。另一件事是
    $save=“插入$db\u name($fccountry\u id,$$fappellation\u name)”
    有两个
    $
    符号表示
    $$fappellation\u name
    另外,您不包括
    值和列。即:
    $save=“插入$db\u name(column1,column2)值($fccountry\u id,$fappellation\u name)”
    Fred,你能详细说明一下如何在html的combobox元素中命名一个名为country_id的元素吗?非常感谢你,我真的被困在这里了。谢谢你,Spencer,工作完美无瑕。insert mysql语句中缺少了几个单引号。工作完美再次感谢你!!哦,对不起,我会把它们添加进来的。无论如何,没问题。我还建议您在第一次打开连接时添加一个检查。这可以通过以下方式完成:
    如果($cn->connect\u error){die($cn->connect\u errno.
    。$cn->connect\u error);/*或任何其他您希望死亡的消息。*/}刚刚更新了MySQL语句中的错误,并将未定义的SturnID ID从“代码>未知国家代码”更改为“<代码> > <代码>未知国家。< /代码>,请考虑接受该答案,如果您满意,请将其进行投票。如果答案不被接受,它将作为未回答的问题而打开。
    <form name="New Appellation" action="actions/register_appellation.php" method="POST"    onsubmit="return validate()">
    Nombre denominacion de origen:
    <input type="text" placeholder="Nombre DO" name="fappellation_name">
    <br>
    Pais denominacion de origen:
    <?php
    include_once 'includes/fast_conn.php';
    $boolSelectionSuccessful = $cn->select_db($db_name);
    if(!$boolSelectionSuccessful){
        $deathMessage = "<br><div>" .
            "Oh no! An error occurred on our end!<br>" .
            "Here's what we know:<hr>" .
            $cn->error .
            "<hr>" .
            "If this error continues to occur, please contact the website administrator." .
            "</div>";
        die($deathMessage);
    }
    $sql = "SELECT `country_name`, `country_id` FROM `countries`";
    $rs = $cn->query($sql);
    if($rs === FALSE){
        $deathMessage = "<br><div>" .
            "Oh no! An error occurred on our end!<br>" .
            "Here's what we know:<hr>" .
            $cn->error .
            "<hr>" .
            "If this error continues to occur, please contact the website administrator." .
            "</div>";
        die($deathMessage);
    }
    echo "<select name='appellation_country'>";
    while($row = $rs->fetch_assoc()){
        echo "<option value='".$row["country_id"]."'>".$row["country_name"]."</option>";
    }
    $rs->close();
    echo "</select>";
    ?>
    <input type="submit" value="Registrar">
    </form>
    
    <?php
    include_once '../includes/fast_conn.php';
    
    function checkField($fieldName){
        if(isset($_POST[$fieldName])){
            if(!empty($_POST[$fieldName])){
                /* The request has the fieldName submitted && the fieldName was not left empty */
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    
    $checkArray = array(checkField('fappellation_name'), checkField('appellation_country'));
    if(in_array(false, $checkArray) || !is_numeric($_POST['appellation_country'])){
        /* the check assumes that 'appellation_country' (country id) should be numeric */
        die("<font size='+1' color='red'>Registration Failed. Please fill in all forms.</font>");
    }
    
    $boolSelectionSuccessful = $cn->select_db($db_name);
    if(!$boolSelectionSuccessful){
        $deathMessage = "<br><div>" .
            "Oh no! An error occurred on our end!<br>" .
            "Here's what we know:<hr>" .
            $cn->error .
            "<hr>" .
            "If this error continues to occur, please contact the website administrator." .
            "</div>";
        die($deathMessage);
    }
    $sql = "SELECT `country_name`, `country_id` FROM `countries`";
    $rs = $cn->query($sql);
    if($rs === FALSE){
        $deathMessage = "<br><div>" .
            "Oh no! An error occurred on our end!<br>" .
            "Here's what we know:<hr>" .
            $cn->error .
            "<hr>" .
            "If this error continues to occur, please contact the website administrator." .
            "</div>";
        die($deathMessage);
    }
    
    $countryIdValid = False;
    $countryName = "";
    /* $countryName is unused, by the current code, but it will still be set in the while loop.
       If you want to store the country name in the `appeallations` table, this variable
       could simply be added to the insert statement. */
    
    while($row = $rs->fetch_assoc()){
        if($row['country_id'] == $_POST['appellation_country']){
            $countryIdValid = true;
            $countryName = $row['country_name'];
            break;
        }
        /* else - continue looping to check the next country_id */
    }
    
    $rs->close();
    
    /* now that the loop has been exited, we'll make sure the country_id was verified */
    if(!$countryIdValid){
        die("<font size='+1' color='red'>Registration Failed. Unknown country.</font>");
    }
    
    $fappellation_name= $cn->real_escape_string($_POST['fappellation_name']);
    $fcountry_id = $cn->real_escape_string($_POST['appellation_country']);
    $save="INSERT INTO `appellations` (`country_id`, `appellation_name`) VALUES ('$fcountry_id', '$fappellation_name')";
    $result = $cn->query($save);
    if($result !== FALSE){
        echo "<font size='+1' color='blue'> " . $_POST['fappellation_name'] . ", Successfully Registered.</font>";
    }else{
        $deathMessage = "<br><div>" .
            "Oh no! An error occurred on our end!<br>" .
            "Here's what we know:<hr>" .
            $cn->error .
            "<hr>" .
            "If this error continues to occur, please contact the website administrator." .
            "</div>";
        die($deathMessage);
    }
    ?>