过帐带有可选和必填字段的表单

过帐带有可选和必填字段的表单

问题描述:

如果我具有必填字段和可选字段,如何从表单中将信息添加到数据库中?到目前为止,我仅作了几处插入,但在所有这些中,所有信息始终存在.在这种情况下,我要填写一份联系表,其中姓名,姓氏和手机号码是必填项,其他所有均为可选项.

How can I add to my database information from a form if I have mandatory and optional fields? So far I have only done a few inserts but in all of those all the information was always present. In this case I am making a contact form where the name, surname and cellphone number are mandatory and where all the others are optional.

此人的信息(姓名,姓氏,昵称)也进入表格,而数字(家庭,工作,传真,手机等)进入另一个表格.

Also the person's information (name, surnames, nickname) goes into a table while the numbers (home, work, fax, cell etc) goes into another table.

我用于POST的(另一个应用程序的)示例代码:

A example (of another app) code I have used for POST:

jQuery(Ajax)

$("#btnAceptar").click(function() {
  var codigolab = $('#txtNumLab').val(),
  capacidad = $('#txtCapacidad').val(),
  carrera = $('#txtCarrera').val(),
  ubicacion = $('#txtUbicacion').val();


var request = $.ajax({
url: "includes/functionsLabs.php",
type: "post",
data: {

    'call': 'addLab',
    'pCodigoLab':codigolab,
    'pCapacidad':capacidad,
    'pCarrera':carrera,
    'pUbicacion':ubicacion},

    dataType: 'html',

   success: function(response){
    $('#info').html(response);


     }
  });
});

在此示例中使用的PHP添加到数据库中

function addLab(){     
if(isset($_POST['pCodigoLab']) && 
    isset($_POST['pCapacidad']) &&
    isset($_POST['pCarrera']) &&
    isset ($_POST['pUbicacion'])){

    $codigolab = $_POST['pCodigoLab'];
    $capacidad = $_POST['pCapacidad'];
    $carrera = $_POST['pCarrera'];
    $ubicacion = $_POST['pUbicacion'];


    $query = "INSERT INTO labs VALUES" . "('null', '$codigolab', '$capacidad', '$carrera','$ubicacion', '1')";

    $result = do_query($query);

    echo "<p>¡El laboratorio ha sido añadido exitosamente!</p>";


}

}

在这种情况下,我将某些字段设为必填字段,而另一些字段则不需要插入到不同的表中,为了在该查询中添加其他插入内容,我是否仅使用AND或类似的名称?

In this case where I am making some fields mandatory and others not I need to insert into different tables, to add another insert into that query do I just use a AND or something like that?

如果用户在某些字段中未输入任何内容,我是否将可选字段保留在isset语句之外?如果我不确定是否要使用这些变量,该如何声明?

Also if the user doesn't input anything in some fields do I leave the optional fields out of the isset statements? How would those variables be declared if I cannot be certain if they're going to be used?

很抱歉,如果我很难做到这一点,我会感到困惑,英语不是我的主要语言.

Sorry if I am making this a bit hard to follow I am a bit confused and english is not my main language.

谢谢.

实际代码的字段:

按Guardar按钮将显示必填字段,其他字段是可选字段.

谢谢

假设我有nameemailnote字段,其中前两个是必填字段,而最后一个不是必需字段.

Let's say I have the fields name, email, note where the first 2 are required and the last one is not.

这是一种实现方法:

<?php
$error = array();
if (!isset($_POST['name']))
{
    $error[] = "The field name was not filled!";
}

if (!isset($_POST['email']))
{
    $error[] = "The field email was not filled!";
}

if (sizeof($error) == 0)
{
    // insert the data
    $query = sprintf("INSERT INTO users VALUES ('%s', '%s', '%s')",
                     mysql_real_escape_string($_POST['name']),
                     mysql_real_escape_string($_POST['email']),
                     mysql_real_escape_string((isset($_POST['note']) ? $_POST['note'] : '')));

    $result = do_query($query);
    echo "<p>¡El laboratorio ha sido añadido exitosamente!</p>";
}
else
{
    // print the error
    foreach ($error as $msg)
    {
        echo $msg, "<br>\n";
    }
}

对于我使用了三元运算符:

isset($_POST['note']) ? $_POST['note'] : ''

如果/否则本质上是一个衬纸,可以写成:

Which essentially is a one liner if/else, which could have been written as:

if (isset($_POST['note']))
{
    $note = $_POST['note'];
}
else
{
    $note = '';
}

还要确保使用mysql_real_escape_string清理数据以防止SQL注入.

Also make sure you sanitize your data in your case using mysql_real_escape_string to prevent SQL Injection.