如何在MYSQL中回显存储为数组的值?

时间:2015-05-27 20:54:38

标签: php mysql arrays echo

Hello目前我有一个MYSQL数据库,我在其中发布表单数据。然后我使用PHP来回显数据并查看表单信息。最近我添加了一个复选框,复选框的值存储为ARRAY。当我回到我的数据库时,我得到的输出是

名称,日期,阵列,阵列

我没有得到ARRAY这个词,而是想获得提交的价值。我知道你应该使用implode命令,但我无法想出一种方法将它合并到我的代码中。检索代码如下所示。使用field_10,field_11,field_12只返回单词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);

  ?>

表单处理代码是这个

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

?>

表格本身

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

2 个答案:

答案 0 :(得分:2)

无法在MySQL数据库中存储PHP数组(因为它的类型),但您可以将其转换为字符串并存储它。当然,您可以稍后将此字符串转换回数组。您应该更改数据库字段以键入var request = require('request'); var url = 'https://www.example.com/api/'; var data = { "jsonrpc":"2.0", "id":"3" }; request.post({url:url, formData: data}, function(err, httpResponse, body) { if (err) { return console.error('post failed:', err); } console.log('Post successful! Server responded with:', body); }); 以在其中存储数组。

要以文本形式存储数组,您可以使用LONGTEXT

serialize

或者使用JSON(在数据库中更好的可读性):

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

答案 1 :(得分:1)

嘿伙计们我只是想发布完整的代码包装,其他人都遇到了麻烦。我将序列化函数添加到我的表单处理部分。

everyWhere

奇怪的是,当你反序列化它时。它只是再次输出单词数组。