user3856234
user3856234

Reputation: 63

How To Echo Values Stored As Array in MYSQL?

Hello Currently I have a MYSQL DB in which I post form data into. I then USE PHP to echo the data out and see the form info. Recently I added a check box and the values of the check box get stored as an ARRAY. When I go to echo my DB the output I get is

Name,Date,Array,Array

Instead of getting the word ARRAY I would like to get the value of the submission. I know you are supposed to use the implode command but I could not figure out a way to incorporate it into my code. The retrieval code looks like this. With field_10,field_11,field_12 returning just the word ARRAY.

<html>
<head>
</head>
<body>
<style>
body {background-color:#DAD8EE}
</style>
<img class="formInfo" src="ree.jpg" alt="" style="width:1000px;">
 <?

$con = mysql_connect("localhost","dk1","root1");

if (!$con)
{
 die('Could not connect: ' . mysql_error());
}

mysql_select_db("usersignup", $con);


$query = "SELECT * FROM `users`";

$comments = mysql_query($query);

echo "<h1> User Sign Ups </h1>";



while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{

$company=$row['field_1'];
$name=$row['field_2'];
$contact_numb=$row['field_3'];
$email_adress=$row['field_4'];
$conf_yr=$row['field_5'];
$dt_arrival=$row['field_6'];
$arrival_tm=$row['field_7'];
$dt_depart=$row['field_8'];
$departure_tm=$row['field_9'];
$dnr_one=$row['field_10'];
$dnr_two=$row['field_11'];
$cmd_shw=$row['field_12'];


$company = htmlspecialchars($row['field_1'],ENT_QUOTES);
$name = htmlspecialchars($row['field_2'],ENT_QUOTES);
$contact_numb = htmlspecialchars($row['field_3'],ENT_QUOTES);
$email_adress = htmlspecialchars($row['field_4'],ENT_QUOTES);
$conf_yr = htmlspecialchars($row['field_5'],ENT_QUOTES);
$dt_arrival = htmlspecialchars($row['field_6'],ENT_QUOTES);
$arrival_tm = htmlspecialchars($row['field_7'],ENT_QUOTES);
$dt_depart= htmlspecialchars($row['field_8'],ENT_QUOTES);
$departure_tm = htmlspecialchars($row['field_9'],ENT_QUOTES);
$dnr_one= htmlspecialchars($row['field_10'],ENT_QUOTES);
$dnr_two= htmlspecialchars($row['field_11'],ENT_QUOTES);
$cmd_shw= htmlspecialchars($row['field_12'],ENT_QUOTES);


echo "  <div style='margin:30px 0px;'>
    <ul>
  <li>Name: $name<br /></li>
  <li>Company: $company<br /></li>
  <li>Contact Number: $contact_numb<br /></li>
  <li> Email Adress: $email_adress<br /></li>
  <li> Conference Year: $conf_yr<br /></li>
  <li> Date of Arrival: $dt_arrival<br /></li>
  <li> Arrival Time: $arrival_tm<br /></li>
  <li> Date of Departure: $dt_depart<br /></li>
  <li> Departure Time: $departure_tm</br></li>
  <li> Dinner on the 14th?: $dnr_one</br></li>
  <li> Dinner on the 15th?: $dnr_two</br></li>
  <li> Comedy Show?: $cmd_shw</br></li>
  </div>
   ";
    }


  mysql_close($con);

  ?>

The form processing code is this

<?php

  $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

include("config.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
$query = "INSERT into `".$db_table."`  (field_1,field_2,field_3,field_4,field_5,field_6,field_7,field_8,field_9,field_10,field_11,field_12) VALUES ('" . $_POST['field_1'] . "','" . $_POST['field_2'] . "','" . $_POST['field_3'] . "','" . $_POST['field_4'] . "','" . $_POST['field_5'] . "','" . $_POST['field_6'] . "','" . $_POST['field_7'] . "','" . $_POST['field_8'] . "','" . $_POST['field_9'] . "','" . $_POST['field_10'] . "','" . $_POST['field_11'] . "','" . $_POST['field_12'] . "')";
mysql_query($query);
mysql_close($link);

include("confirms.html");

?>

The Form itself

<div id="mainForm">




        <div id="formHeader">
        <img class="formInfo" src="ree.jpg" alt="" style="width:1000px;">
                <h2 class="formInfo">DEMO Conference Sign Up </h2>
                <p class="formInfo">DEMO </p>
        </div>


        <BR/><!-- begin form -->
        <form method=post enctype=multipart/form-data action=processors.php onSubmit="return validatePage1();"><ul class=mainForm id="mainForm_1">

                <li class="mainForm" id="fieldBox_1">
                    <label class="formFieldQuestion">Company Name &nbsp;*</label><input class=mainForm type=text name=field_1 id=field_1 size='20' value=''></li>

                <li class="mainForm" id="fieldBox_2">
                    <label class="formFieldQuestion">Guest Name &nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Enter Guest Name here</span></a></label><input class=mainForm type=text name=field_2 id=field_2 size='20' value=''></li>

                <li class="mainForm" id="fieldBox_3">
                    <label class="formFieldQuestion">Contact Number &nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Enter Phone Number here </span></a></label><input class=mainForm type=text name=field_3 id=field_3 size='20' value=''></li>

                <li class="mainForm" id="fieldBox_4">
                    <label class="formFieldQuestion">Email Address&nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Enter Email Address </span></a></label><input class=mainForm type=text name=field_4 id=field_4 size='20' value=''></li>

                <input type='hidden' class="mainForm" id="fieldBox_5" name="field_5" value='2015'> 

                <li class="mainForm" id="fieldBox_6">
                    <label class="formFieldQuestion">Date of Arrival&nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Please select your date of arrival </span></a></label><select class=mainForm name=field_6 id=field_6><option value=''></option><option value="October 13 (Pre Conference)">October 13 (Pre Conference)</option><option value="October 14 (Day 1)">October 14 (Day 1)</option><option value="October 15 (Day 2)">October 15 (Day 2)</option><option value="October 16 (Final Day)">October 16 (Final Day)</option></select></li>

                <li class="mainForm" id="fieldBox_7">
                    <label class="formFieldQuestion">Enter Your Arrival Time&nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Please Enter the Time you will be arriving to the airport </span></a></label><input class=mainForm type=text name=field_7 id=field_7 size='20' value=''></li>

                <li class="mainForm" id="fieldBox_8">
                    <label class="formFieldQuestion">Date of Departure &nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Enter the date you will be leaving the conference</span></a></label><select class=mainForm name=field_8 id=field_8><option value=''></option><option value="October 13 (Pre Conference)">October 13 (Pre Conference)</option><option value="October 14 (Day 1)">October 14 (Day 1)</option><option value="October 15 (Day 2)">October 15 (Day 2)</option><option value="October 16 (Final Day)">October 16 (Final Day)</option></select></li>

                <li class="mainForm" id="fieldBox_9">
                    <label class="formFieldQuestion">Time of Departure &nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Please enter what time you will be departing the conference </span></a></label><input class=mainForm type=text name=field_9 id=field_9 size='20' value=''></li>

                <li class="mainForm" id="fieldBox_10">
                    <label class="formFieldQuestion">Dinner October 14th &nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Would you like to attend dinner with INFORM applications staff? </span></a></label><span><input class=mainForm type=checkbox name=field_10[] id=field_10_option_1 value="Yes" /><label class=formFieldOption for="field_10_option_1">Yes </label><input class=mainForm type=checkbox name=field_10[] id=field_10_option_2 value="No " /><label class=formFieldOption for="field_10_option_2">No </label></span></li>

                <li class="mainForm" id="fieldBox_11">
                    <label class="formFieldQuestion">Dinner October 15th &nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Would you like to attend dinner with members of the INFORM staff? </span></a></label><span><input class=mainForm type=checkbox name=field_11[] id=field_11_option_1 value="Yes " /><label class=formFieldOption for="field_11_option_1">Yes </label><input class=mainForm type=checkbox name=field_11[] id=field_11_option_2 value="No " /><label class=formFieldOption for="field_11_option_2">No </label></span></li>

                <li class="mainForm" id="fieldBox_12">
                    <label class="formFieldQuestion">Do You Want To Attend The Comedy Show ?&nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>A comedy show will be held at the Borgata on Wednesday the 14th  </span></a></label><span><input class=mainForm type=checkbox name=field_12[] id=field_12_option_1 value="Yes " /><label class=formFieldOption for="field_12_option_1">Yes </label><input class=mainForm type=checkbox name=field_12[] id=field_12_option_2 value="No " /><label class=formFieldOption for="field_12_option_2">No </label></span></li>


        <!-- end of this page --->

        <!-- page validation -->
        <SCRIPT type=text/javascript>
        <!--
            function validatePage1()
            {
                retVal = true;
                if (validateField('field_1','fieldBox_1','text',1) == false)
 retVal=false;
if (validateField('field_2','fieldBox_2','text',1) == false)
 retVal=false;
if (validateField('field_3','fieldBox_3','menu',1) == false)
 retVal=false;

                if(retVal == false)
                {
                    alert('Please correct the errors.  Fields marked with an asterisk (*) are required');
                    return false;
                }
                return retVal;
            }
        //-->
        </SCRIPT>

        <!-- end page validaton -->



        <!-- next page buttons --><li class="mainForm">
                    <input id="saveForm" class="mainForm" type="submit" value="Submit" />
                </li>

            </form>
            <!-- end of form -->
        <!-- close the display stuff for this page -->

Upvotes: 2

Views: 274

Answers (2)

user3856234
user3856234

Reputation: 63

Hey guys I just wanted to post the complete code encase anybody else is having trouble. I added the serialize function to my form processing part.

<?php
$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

include("config.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
$query = "INSERT into `".$db_table."` (field_1,field_2,field_3,field_4,field_5,field_6,field_7,field_8,field_9,field_10,field_11,field_12) VALUES ('" . $_POST['field_1'] . "','" . $_POST['field_2'] . "','" . $_POST['field_3'] . "','" . $_POST['field_4'] . "','" . $_POST['field_5'] . "','" . $_POST['field_6'] . "','" . $_POST['field_7'] . "','" . $_POST['field_8'] . "','" . $_POST['field_9'] . "','" . serialize($_POST['field_10']) . "','" .serialize($_POST['field_11']) . "','" .serialize($_POST['field_12']) . "')";
mysql_query($query);
mysql_close($link);

include("confirms.html");
?>

Strangely when you unserialize it. It just outputs the word array again.

Upvotes: 1

Richard
Richard

Reputation: 2815

It is not possible to store an PHP array in a MySQL database (because of it's type), but you can convert it to string and store this. Of course, you can convert this string later back to an array. You should change a database field to type LONGTEXT to store an array in it.

To store an array in text form, you can use serialize:

$arr = serialize($arr); // To save it in DB
$arr = unserialize($arr); // To get array after select

Or use JSON (better readable in database):

$arr = json_encode($arr); // To save it in DB
$arr = json_decode($arr); // To get array after select

Upvotes: 2

Related Questions