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

}
Ankur Aggarwal
  • 2,993
  • 5
  • 30
  • 56
user3393658
  • 23
  • 1
  • 1
  • 5
  • 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](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has many examples in detail. You can also see http://bobby-tables.com/php for alternatives and explanation of the danger. – Andy Lester Apr 15 '14 at 19:27
  • That is a good suggestion. I will consider your tip and try to upgrade my code. – user3393658 Apr 15 '14 at 19:50

2 Answers2

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

showdev
  • 28,454
  • 37
  • 55
  • 73
  • This method does not do any changes. I see the same output on email as before name: Krishna dept: CS email: sainth.r@gmail.com phone: 9015518456 comments: model: Array tag: Array itemdesc: Array Submit: Submit – user3393658 Apr 15 '14 at 20:24
  • Sorry, I had a typo in the code. I used your array structure and it's working here: http://codepad.viper-7.com/l48AMK – showdev Apr 15 '14 at 20:29
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>';
  • This method is giving the output as list of array instead of ordered content. Array ( [name] => Krishna [dept] => CS [email] => sainth.r@gmail.com [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 ) – user3393658 Apr 15 '14 at 20:20