2

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 -->
user3856234
  • 63
  • 1
  • 10
  • How are they stored in the database? MySQL can't store native PHP arrays directly, they'd have to be serialised or json_encoded or similar – Mark Baker May 27 '15 at 20:55
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 27 '15 at 20:58
  • @MarkBaker The DB has VARCHAR 50. If you look at myPHPadmin it just says field_10 Array. – user3856234 May 27 '15 at 21:01
  • What is actually stored in the database? If it's the word `Array` then the problem is with storing the data.... and you shouldn't really try to store array values in a single column, but normalise your database properly – Mark Baker May 27 '15 at 21:03
  • @MarkBaker is there a way to check what is being sent to the DB before it gets to the processing? Can I submit the values somewhere else and just print them? – user3856234 May 27 '15 at 21:06
  • Check what's actually stored in the database by running the query from the command line, or via a GUI tool such as phpmyadmin.... but from the look of your forms and the INSERT, you're not storing the value correctly at all – Mark Baker May 27 '15 at 21:12
  • @MarkBaker when I look at the DB using phpMyAdmin under field_10 I see the word Array. Does that mean the type of data is an array or is the word Array being stored there? – user3856234 May 27 '15 at 21:14
  • No it isn't a data type of array (databases don't have arrays as a datatype, varchar is just a string).... You've stored the word `Array` because that's what PHP does when you try to treat an array as a string, like if you try to echo an array – Mark Baker May 27 '15 at 21:31

2 Answers2

2

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
Richard
  • 2,840
  • 3
  • 25
  • 37
  • i was just confused about how to implement this exactly. The $arr here would be the Value I am submitting in my POST right? Would I be adding this into the form to encode the data before it gets sent in for processing? – user3856234 May 27 '15 at 21:17
  • @user3856234: You should do something like the following: `$db->query("INSERT INTO table (array) VALUES ('" . $db->real_escape_string(json_encode($_POST['array'])) . "')");` – Richard May 27 '15 at 21:20
1

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.

user3856234
  • 63
  • 1
  • 10