数据库字段,必填

时间:2014-02-18 21:42:04

标签: php database printing required

我试图询问所需的字段打印以确保其正常工作。     但这个问题被评为如此糟糕,以至于无法提出更多问题。     这是它的解决方案,尝试对其进行评级,以便我可以删除阻止。

 <?php   if (empty($_POST) === false) {  
    $required_fields = array(
        'username', 'password', 'password_again', 
        'first_name', 'last_name', 
        'email', 'bday', 
        'county', 'zip', 'gender'
    );
    // create our output array
    $output = array(); 
    $altOutput = array();

    // this adds the post value to our output array
    // using a foreach loop
    // it simply looks for the POST by the field in your array
    // if it's found and not empty, it will add to our new output array
    foreach ($required_fields as $field) 
        if (!empty($_POST[$field])) {
            // this adds the field to your new output array
            $output[] = $_POST[$field];
            // this removes it from the POST array
            unset($_POST[$field]);
        }

    // now output the new array having only the fields you want
    echo '<pre>', print_r($output, true), '</pre>';

    // if you want to print out what's left over
    echo '<pre>', print_r($_POST, true), '</pre>';}

3 个答案:

答案 0 :(得分:0)

您似乎在理解代码方面遇到了一些麻烦。您的$required_fields数组甚至没有被再次使用。我花了一些时间来完成这项工作:

$required_fields = array(username, password, password_again, first_name, last_name, email, bday, county, zip, gender);
foreach($required_fields as $field) {
    print $_POST[$field];
}

请注意,我们正在遍历$required_fields数组并从$_POST中提取单个数据。

答案 1 :(得分:0)

So, I just noticed, despite having fully answered the question. Despite even 
being selected as correct answer. The 3 upvotes I had gained on this lost the 
final mark today, thus putting this answer at 0. I also noticed this question 
is now marked closed. Could someone please explain to me why an answered 
question is closed after the fact and why the selected correct answer is 
steadily being downvoted?


它正如你所说的那样,因为这就是你为它设置的目的。

快速分解:

<?php // init php code
    // here you check if post has anything in it's array
    // that's right, $_POST will be an ARRAY
    if (empty($_POST) === false) { // same as `if (!empty($_POST)) {` except longer
        // here you simply set an array variable
        $required_fields = array(username, password, password_again, first_name, last_name, email, bday, county, zip, gender);
        // here you're one lining it, but suffice it to say, you're sending a <pre> tag to the browser
        // which helps to make what `print_r` produces more readable as you're printing an array
        echo '<pre>', print_r($_POST, true), '</pre>';

您可能已经注意到,您甚至没有使用数组$required_fields

现在

如果您想打印这些字段中的每一个并且只打印那些字段作为阵列,那么您需要这样做

<?php   
if (empty($_POST) === false) {  
        $required_fields = array(
            'username', 'password', 'password_again', 
            'first_name', 'last_name', 
            'email', 'bday', 
            'county', 'zip', 'gender'
        );

        // create our output array
        $output = array(); 
        // $altOutput = array(); // not used in this example

        // this adds the post value to our output array
        // using a foreach loop
        // it simply looks for the POST by the field in your array
        // if it's found and not empty, it will add to our new output array
        foreach ($required_fields as $field) 
            if (!empty($_POST[$field])) {
                // this adds the field to your new output array
                $output[] = $_POST[$field];
                // this removes it from the POST array
                unset($_POST[$field]);
            }

        // now output the new array having only the fields you want
        echo '<pre>', print_r($output, true), '</pre>';

        // if you want to print out what's left over
        echo '<pre>', print_r($_POST, true), '</pre>';
}

答案 2 :(得分:-1)

这是一个完整的示例,如果一个或多个字段为空,则取消进一步执行,并向用户打印出这些字段的名称:

if( !empty( $_POST ) )
{
    $requiredFields = array(
        'username', 
        'password', 
        'password_again', 
        'first_name', 
        'last_name', 
        'email', 
        'bday', 
        'county', 
        'zip', 
        'gender',
    );
    $missingFields = array();

    foreach( $requiredFields as $fieldName )
    {
        if( empty( $_POST[$fieldName] ) )
        {
            $missingFields[] = $fieldName;
        }
    }

    if( !empty( $missingFields ) )
    {
        echo sprintf( 'You have not filled out the following fields: %s.', implode( ', ', $missingFields ) );
        return;
    }
}