【问题标题】:multiple SQL insertion & form validation In phpphp中的多个SQL插入和表单验证
【发布时间】:2012-01-12 01:08:23
【问题描述】:

我正在尝试使用 php 和 mysql 使用表单添加多个产品,但我对执行这些操作的概念感到困惑,

我的预期输出是,提供一个包含至少十行多个字段的表单,并在其中进行验证,如果没有错误则继续插入。

到目前为止,这是我对单一表单插入的理解:

    $add_errors = array();

//if there is a post request
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

// do some validation
if (empty($_POST['name'])) {
        $add_errors['name'] = 'Please enter the name!';
}

if (empty($_POST['description'])) {
        $add_errors['description'] = 'Please enter the description!';
}

if (empty($add_errors)) { // If everything's OK.
//do the insertion
$q = 'INSERT INTO ........')';
}

}//end of form submission


echo '<form action="product_add.php" enctype="multipart/form-data" method="post" accept-charset="utf-8">';
echo '<input type=..... name=...... id=.....>';
echo '<input type=..... name=...... id=.....>';
echo '<input type=..... name=...... id=.....>';
echo '</form';
//this form is only a single row with multiple column(field) ,I am trying to make it into multiple column

【问题讨论】:

    标签: php mysql validation insertion


    【解决方案1】:

    我会重写上面的代码...我要在这里重写它:

    <?php
    $rows = 10; // rows desired.
    
    //if there is a post request
    if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    
        while($i < $rows){
            if (empty($_POST['description'.$i])) {
            $add_errors['description'.$i] = 'Please enter the description!';
            }
            // more error checking if needed...
            ++$i;
        }
    
        if (empty($add_errors)) { // If everything's OK.
            //do the insertion
            $q = 'INSERT INTO ........')';
        }
    
    }//end of form submission
    
    echo '<form action="product_add.php" enctype="multipart/form-data" method="post" accept-charset="utf-8">';
    $i = 0;
    while($i < $rows){
        echo '<input type=..... name="description'.$i.'" id=.....>';
        ++$i;
    }
    echo '</form';
    ?>
    

    尝试类似的方法...(我的代码可能有一两个错误,因为我只是在这里写的并且没有测试它)但这是一般的想法。 =)

    【讨论】:

    • 感谢大家为我指明正确的方向。我想我可能明白你的大致想法 =)
    猜你喜欢
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多