如何回显 MYSQL 中存储为数组的值?
How To Echo Values Stored As Array in MYSQL?
您好,目前我有一个 MYSQL 数据库,我在其中 post 形成数据。然后我使用 PHP 回显数据并查看表单信息。最近我添加了一个复选框,复选框的值存储为一个数组。当我回显我的数据库时,我得到的输出是
名称、日期、数组、数组
我不想获取 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 *</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 * <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 * <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 * <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 * <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 * <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 * <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 * <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 * <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 * <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 ? * <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 -->
无法将 PHP 数组存储在 MySQL 数据库中(因为它的类型),但您可以将其转换为字符串并存储它。当然,您可以稍后将此字符串转换回数组。您应该将数据库字段更改为键入 LONGTEXT
以在其中存储一个数组。
要以文本形式存储数组,可以使用serialize
:
$arr = serialize($arr); // To save it in DB
$arr = unserialize($arr); // To get array after select
或使用JSON(在数据库中可读性更好):
$arr = json_encode($arr); // To save it in DB
$arr = json_decode($arr); // To get array after select
大家好,我只是想 post 完整的代码来解决其他人遇到的问题。我将序列化功能添加到我的表单处理部分。
<?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");
?>
反序列化时很奇怪。它只是再次输出单词数组。
您好,目前我有一个 MYSQL 数据库,我在其中 post 形成数据。然后我使用 PHP 回显数据并查看表单信息。最近我添加了一个复选框,复选框的值存储为一个数组。当我回显我的数据库时,我得到的输出是
名称、日期、数组、数组
我不想获取 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 *</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 * <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 * <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 * <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 * <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 * <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 * <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 * <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 * <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 * <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 ? * <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 -->
无法将 PHP 数组存储在 MySQL 数据库中(因为它的类型),但您可以将其转换为字符串并存储它。当然,您可以稍后将此字符串转换回数组。您应该将数据库字段更改为键入 LONGTEXT
以在其中存储一个数组。
要以文本形式存储数组,可以使用serialize
:
$arr = serialize($arr); // To save it in DB
$arr = unserialize($arr); // To get array after select
或使用JSON(在数据库中可读性更好):
$arr = json_encode($arr); // To save it in DB
$arr = json_decode($arr); // To get array after select
大家好,我只是想 post 完整的代码来解决其他人遇到的问题。我将序列化功能添加到我的表单处理部分。
<?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");
?>
反序列化时很奇怪。它只是再次输出单词数组。