未填写的表单值通过 php 在 mysql 中创建空行

发布于 2024-11-19 13:54:27 字数 1039 浏览 0 评论 0原文

第一次发海报,潜伏已久:) 我有一个表单设置,它将具有七列的单个表中的多行数据发布到 php 函数,以将其作为新记录插入数据库中。该表单运行良好,但是,如果表单中的某一行未填写,则 php 函数将在数据库中创建一个空白行。我正在尝试找出如何避免这种情况。我确信它与表单本身无关,而与 php.ini 有关。请看一下。我完全愿意接受建议。

<?php
require("header.php");
require("dbinc.php");
foreach($_POST['card'] as $row=>$cardcounted) 
{
    $model=$_POST['model'];
    $serial=$_POST['serial'][$row];
    $card=$cardcounted;
    $status=$_POST['status'];
    $date=$_POST['date'];
    $location=$_POST['location'];
    $query = mysql_query("INSERT INTO receivers (`id`, `model`, `serial`, `card`, `status`, `date`, `location`) VALUES ('null','$model','$serial','$card','$status','$date','$location')");

    if(!isset($serial[$row]) || $serial[$row] == '') {
        // error message here, redisplay form if desired
    } else {
        // no errors - process data
    } 

    if (!$query)
    {
        die('Error: ' . mysql_error());
    }
}
echo $row+1 . " record(s) added";

mysql_close()
?>

我在序列号上按行添加了 !isset 以检查空帖子,但不确定如何正确合并它。我认为我走在正确的轨道上,只需要一点点推动:)

First time poster, long time lurker :)
I have a form setup that posts data for multiple rows in a single table with seven colums to a php function to insert it as new records in a database. The form is working great, however, if one of the rows in the form is left unfilled then the php function is creating a blank row in the database. I am attempting to figure out how to avoid this. I am certain it has nothing to do with the form itself, and rather has to do with the php. Please have a look at it. I'm totally open to suggestions.

<?php
require("header.php");
require("dbinc.php");
foreach($_POST['card'] as $row=>$cardcounted) 
{
    $model=$_POST['model'];
    $serial=$_POST['serial'][$row];
    $card=$cardcounted;
    $status=$_POST['status'];
    $date=$_POST['date'];
    $location=$_POST['location'];
    $query = mysql_query("INSERT INTO receivers (`id`, `model`, `serial`, `card`, `status`, `date`, `location`) VALUES ('null','$model','$serial','$card','$status','$date','$location')");

    if(!isset($serial[$row]) || $serial[$row] == '') {
        // error message here, redisplay form if desired
    } else {
        // no errors - process data
    } 

    if (!$query)
    {
        die('Error: ' . mysql_error());
    }
}
echo $row+1 . " record(s) added";

mysql_close()
?>

I added in the !isset on the serial numbers by row to check for null posts but am unsure as to how to incorporate that properly. i think im on the right track, just need that little push :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝情姑娘 2024-11-26 13:54:27

第一个选择是使用 JavaScript 验证来找出无效的表单提交,有些用户可能会禁用 JavaScript,因此始终建议进行服务器端验证

所以编写一个方法,例如

isValid($postArray) {
  foreach($postArray as $key => $value) {
    if ($value == "") {
       return false;    
    }
  }
  return true;
}

如果表单字段名称和数据库列名称相似,则可以使用

$sql = "INSERT INTO receivers ";
$keys = "(";
$values = "(";
foreach($postArray as $key => $value) {
    if ($value != "") {
       $keys += $key;
       $values += $value;
    }
}

$sql = $sql + $keys +" VALUES "+ $values;

First choice is to use JavaScript validation to find out invalid form submits, Some users may disable JavaScript, so its always recommended to do server-side validations

So write a method like

isValid($postArray) {
  foreach($postArray as $key => $value) {
    if ($value == "") {
       return false;    
    }
  }
  return true;
}

If form field names and db column names are similar you can use

$sql = "INSERT INTO receivers ";
$keys = "(";
$values = "(";
foreach($postArray as $key => $value) {
    if ($value != "") {
       $keys += $key;
       $values += $value;
    }
}

$sql = $sql + $keys +" VALUES "+ $values;
笑咖 2024-11-26 13:54:27

也许尝试将您的 mysql 查询移动到您当前将值插入数据库的 else 中,无论您是否有表单错误。此外,您还需要保护插入数据库的任何内容。现在是一次 SQL 注入实地考察;)

Maybe try moving your mysql query into your else currently you insert values into the database regardless of whether you have form errors or not. Also you will need to secure anything that you are inserting into your database. Its an SQL injection field trip right now ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文