0

I have a application form that accepts array of data. The data is stored efficiently on DB but when I try to put this whole data to my email address. Just the details general details like name, phone number, address are saved. multiple levels of data stored in array are not shown in email, instead they are displayed as "array".

if (isset($_POST["Submit"]))
        { 

            $datetime = $_POST["datetime"];
            $name = $_POST["name"];
            $dept = $_POST["dept"];
            $email = $_POST["email"];
            $phone = $_POST["phone"];
            $comments = $_POST["comments"];

            $cnt = count($_POST['model']);
            for($i=0;$i<$cnt;$i++){             
            $model = $_POST["model"][$i];
            $tag = $_POST["tag"][$i];
            $itemdesc = $_POST["itemdesc"][$i];

            $Query = "INSERT INTO ssubmit (datetime, name, dept, email, phone, comments, model, outag, itemdesc) VALUES (NULL, '$name', '$dept', '$email', '$phone',  '$comments', '$model', '$tag', '$itemdesc')";
            $Result = mysql_query($Query, $Link) or die(mysql_error());
       }

//php code to retrieve the values

foreach($_POST as $var => $value)

{

echo $var . ': <b>' . $value . "</b> \r\n";

}
2
  • 7
    By building SQL statements with outside variables, you are leaving yourself open to SQL injection attacks. Also, any input data with single quotes in it, like a name of "O'Malley", will blow up your SQL query. Please learn about using parametrized queries, preferably with the PDO module, to protect your web app. This question has many examples in detail. You can also see bobby-tables.com/php for alternatives and explanation of the danger. Commented Apr 15, 2014 at 19:27
  • That is a good suggestion. I will consider your tip and try to upgrade my code. Commented Apr 15, 2014 at 19:50

2 Answers 2

1

You can loop through the values of arrays to output the data, similar to what you've done for your database insert.

If your arrays are not multi-dimensional, try using PHP's implode() to concatenate strings from arrays:

foreach($_POST as $var => $value) {

    // if this value is an array, implode it to a string
    if (is_array($value)) { $value = implode(", ",$value); }

    // output this var/value pair
    echo "<p>".$var.": <strong>".$value."</strong></p>";

}

WORKING EXAMPLE

Sign up to request clarification or add additional context in comments.

2 Comments

This method does not do any changes. I see the same output on email as before name: Krishna dept: CS email: [email protected] phone: 9015518456 comments: model: Array tag: Array itemdesc: Array Submit: Submit
Sorry, I had a typo in the code. I used your array structure and it's working here: codepad.viper-7.com/l48AMK
0

Instead of building up the output yourself, use either var_export or print_r. That way, everything will be outputted and you don't have to worry about how deep the array ends up getting.

To output to the browser:

echo '<pre>';
print_r($_POST);
echo '</pre>';

To store in a variable:

$message = '<pre>';
$message .= print_r($_POST, true); // Notice that true is being passed as the second argument
$message .= '</pre>';

1 Comment

This method is giving the output as list of array instead of ordered content. Array ( [name] => Krishna [dept] => CS [email] => [email protected] [phone] => 9015518456 [comments] => [model] => Array ( [0] => Dell [1] => Apple [2] => HP ) [outag] => Array ( [0] => 1245 [1] => 8798 [2] => 1232 ) [itemdesc] => Array ( [0] => Big [1] => Big [2] => Big ) [Submit] => Submit )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.