php - 发布带有可选字段和必填字段的表单

标签 php mysql

如果我有必填字段和可选字段,如何从表单向数据库添加信息?到目前为止,我只做了一些插入,但在所有这些插入中,所有信息始终存在。在本例中,我正在制作一个联系表单,其中姓名和手机号码是必填项,其他所有都是可选的。

此外,此人的信息(名字、姓氏、昵称)会进入一个表,而号码(家庭、工作、传真、手机等)会进入另一个表。

我用于 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 或类似的东西?

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

抱歉,如果我让这有点难以理解,我有点困惑,英语不是我的主要语言。

提前致谢。

实际代码的FIDDLE:

Pressing Guardar button displays the fields that are mandatory the others are optional.

谢谢

最佳答案

假设我有字段 nameemailnote,其中前 2 个是必需的,最后一个不是。

这是一种方法:

<?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";
    }
}

For note I used a ternary operator:

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

这本质上是一个 if/else 语句,可以写成:

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

还要确保使用 mysql_real_escape_string 清理您案例中的数据,以防止 SQL 注入(inject)。

关于php - 发布带有可选字段和必填字段的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23166105/

相关文章:

php - Apigility 不保存文档字段

php - JQuery 和 PHP 验证问题?

php - 我什么时候应该关闭数据库连接?

php - mysql GROUP BY 遇到问题

php - CakePHP 迁移数据库行

php - Laravel 表单请求 : how to avoid n+1 queries when validating arrays?

php - 将数据库中的多个值存储到一个变量中

mysql - 如果没有结果,选择 COUNT (*) 失败

php - 我需要在 MYSQL 查询中的什么地方使用反引号或引号?

Python aiomysql 在我的查询参数周围放置了一组引号