从数据库中检索已保存的复选框的名称和值

时间:2009-01-03 09:56:36

标签: php html-form

我有一个带复选框的表单,每个表都有一个值。当注册用户选择任何  复选框值增加(求和),然后注册用户保存他的

选中复选框,如果他对汇总到数据库的结果感到满意,这一切都很好 ...我希望让注册用户能够通过查找和显示他在页面中选择的复选框来查看他的选择历史...我怎么能这样做?

我只能将选中的复选框保存为选项1,选项2,例如..

我想查看保存在数据库中的所选复选框,当用户第一次选择它们时会显示在页面中:例如,如果注册用户选择这3个选项

  • LEAD DEEP KEEL(1825)
  • 全功能主帆(558)
  • TEAK SIDE DECKS(2889)

它们将被保存为例如(choice1,choice2,choice3)。

但是,如果他想要查看所选的复选框,则首先出现与他们选择的一样:

  • LEAD DEEP KEEL(1825)
  • 全功能主帆(558)
  • TEAK SIDE DECKS(2889)

这是我的用户表:

$query="CREATE TABLE User( 
user_id      varchar(20), 
password     varchar(40),
user_type    varchar(20),
firstname    varchar(30),
lastname     varchar(30),
street   varchar(50),
city         varchar(50),
county   varchar(50),
post_code    varchar(10),
country      varchar(50),
gender   varchar(6),
dob      varchar(15),
tel_no   varchar(50),
vals         varchar(50),
email        varchar(50))"; 

以及用于选择数据库

的选项的代码
<?php

include("databaseconnection.php");

$str = '';

foreach($_POST as $key => $val)

if (strpos($key,'choice') !== false)
 $str .= $key.',';


$query = "INSERT INTO User (vals) VALUES('$str')";
$result=mysql_query($query,$conn);
if ($result)
{
     (mysql_error();
}
else
{  
echo " done";      
}

?>

这是我的表格:

        function checkTotal(){             document.listForm.total.value ='';             var sum = 0;             for(i = 0; i

<form name="listForm" method="post" action="insert_options.php" >
<TABLE cellPadding=3 width=600 border=0>
        <TBODY>

        <TR>
          <TH align=left width="87%" bgColor=#b0b3b4><SPAN 
            class=whiteText>Item</SPAN></TH>
          <TH align=right width="13%" bgColor=#b0b3b4><SPAN 
            class=whiteText>Select</SPAN></TH></TR>
        <TR>
          <TD  bgcolor="#9da8af"colSpan=2><SPAN class=normalText><B>General</B></SPAN></TD></TR>
        <TR>
          <TD  bgcolor="#c4c8ca"><SPAN class=normalText >TEAK SIDE DECKS (2889)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="2889" type="checkbox" onchange="checkTotal()" /></TD></TR>
        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>LEAD DEEP KEEL (1825)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="1825" type="checkbox" onchange="checkTotal()"></TD></TR>

        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>FULLY BATTENED MAINSAIL (558)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="558"  type="checkbox"  onchange="checkTotal()"></TD></TR>

        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>HIGH TECH SAILS FOR CONVENTIONAL RIG (1979)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="1979" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>IN MAST REEFING WITH HIGH TECH SAILS (2539)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="2539" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>SPlNNAKER GEAR (POLE LINES DECK FITTINGS) (820)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="820" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>SPINNAKER POLE VERTICAL STOWAGE SYSTEM (214)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="214" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>GAS ROD KICKER (208)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="208" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>SIDE RAIL OPENINGS (BOTH SIDES) (392)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="392" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>SPRING CLEATS MIDSHIPS -ALUMIMIUM (148)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="148" type="checkbox" onchange="checkTotal()"></TD></TR>


          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>ELECTRIC ANCHOR WINDLASS (1189)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="1189" type="checkbox" onchange="checkTotal()"> </TD></TR>




          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>ANCHOR CHAIN GALVANISED (50m) (202)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="202" type="checkbox" onchange="checkTotal()"> </TD></TR>



          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>ANCHOR CHAIN GALVANISED (50m) (1141)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="1141" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD  bgcolor="#9da8af"colSpan=2><SPAN class=normalText><B>NAVIGATION & ELECTRONICS</B></SPAN></TD></TR>
        <TR>
          <TD  bgcolor="#c4c8ca"><SPAN class=normalText >WIND VANE (STAINLESS STEEL)(41)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="41" type="checkbox" onchange="checkTotal()" /></TD></TR>
        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>RAYMARINE ST6O LOG & DEPTH (SEPARATE UNITS)(226)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="226" type="checkbox" onchange="checkTotal()"></TD></TR>

      <TR>
          <TD  bgcolor="#9da8af"colSpan=2><SPAN class=normalText><B>ENGINES & ELECTRICS</B></SPAN></TD></TR>
        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>SHORE SUPPLY (220V) WITH 3 OUTLETS (EXCLUDJNG SHORE CABLE) (327)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="327"  type="checkbox"  onchange="checkTotal()"></TD></TR>

        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>3rd BATTERY(14OA/H)(196)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="196" type="checkbox" onchange="checkTotal()"></TD></TR>

         <TD bgColor=#c4c8ca><SPAN class=normalText>24 AMP BATTERY CHARGER (475)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="475" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>2 BLADED FOLDING PROPELLER (UPGRADE)(299)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="299" type="checkbox" onchange="checkTotal()"></TD></TR>


         <TR>
          <TD  bgcolor="#9da8af"colSpan=2><SPAN class=normalText><B>BELOW DECKS/DOMESTIC</B></SPAN></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>WARM WATER (FROM ENGINE & 220V)(749)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="749" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>SHOWER IN AFT HEADS WITH PUMPOUT(446)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="446" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>DECK SUCTION DISPOSAL FOR HOLDINGTANK(166)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="166" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>REFRIGERATED COOLBOX (12V)(666)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="666" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>LFS SAFETY PACKAGE (COCKPIT HARNESS POINTS STAINLESS STEEL JACKSTAYS)(208)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="208" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>UPHOLSTERY UPGRADE IN SALOON (SUEDETYPE)(701)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="701" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD  bgcolor="#9da8af"colSpan=2><SPAN class=normalText><B>NAVIGATION ELECTRONICS & ELECTRICS</B></SPAN></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>VHF RADIO AERIAL CABLED TO NAVIGATION AREA(178)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="178" type="checkbox" onchange="checkTotal()"></TD></TR>
</table>

2 个答案:

答案 0 :(得分:3)

在html表单中,而不是

<input type="checkbox" name="choice" value="123">

执行:

<input type="checkbox" name="choice[]" value="123">

这将为您提供数组选择。

然后更改您的PHP代码以将选项插入到数据库中:

<?php

$str='';
foreach ($_POST['checked'] as $val)
{
    $str.=$val.',';
}

$str=substr($str,0,-1);//Remove the final comma from the end

$sql="UPDATE users SET vals='$str' WHERE user_id='$user_id'";
mysql_query($sql) or die(mysql_error());

?>

然后在向用户显示选项时,请执行以下操作:

腓:

<?php

$checked=array();
$sql="SELECT vals FROM users WHERE user_id='$user_id'";
$result=mysql_query($sql) or die(mysql_error());

$row=mysql_fetch_assoc($row);
$vals=explode(',',$row['vals']);


foreach ($vals as $val)
{
    if ($val !='' && is_numeric($val))
        $checked[$val]='checked';
}

function set_checked($value)
{
    global $checked;
    if (isset($checked[$value]))
        return 'checked';
    else
        return '';
}

?>

在html中:

<input type="checkbox" name="choice[]" value="123" <? echo set_checked('123');?>>

PS。请确保将$ user_id替换为用户的用户ID或在查询中识别他的其他方式。

答案 1 :(得分:0)

要将设置保存到数据库中,您必须使用UPDATE,而不是INSERT。首先,你必须知道user_id。然后,您必须发出如下查询:

$query = "update User set val = '" . mysql_real_escape_string($str)
       . "' where user_id = $user_id";

您可以使用PHP的explode函数从字符串中获取值数组。

$values = explode(',', $val);