Dreamweaver 多个复选框选择然后插入 MySQL

Dreamweaver multiple checkbox selection then insert to MySQL

我有一张订单,产品在复选框中。所选项目应插入到数据库列中。我还不太擅长编码,所以如果你给我举个例子就太好了。谢谢。

这是我的桌子:

<form name="form" method="POST">
    <table width="623" height="283" border="0" align="center">
<tr>
<td width="146">Name:</td>
<td width="266"><label for="name"></label>
  <input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>Contact No:</td>
<td><label for="contactno"></label>
  <input type="text" name="contactno" id="contactno" /></td>
</tr>
<tr>
<td>Address:</td>
<td><label for="address"></label>
  <input type="text" name="address" id="address" /></td>
</tr>
<tr>
<td height="31">Select Items:</td>
<td>
    <label>
      <input type="checkbox" name="CheckboxGroup[]" value="sample one" id="CheckboxGroup_0" />
      Checkbox</label>
    <br />
    <label>
      <input type="checkbox" name="CheckboxGroup[]" value="sample two" id="CheckboxGroup_1" />
      Sample</label>
    <br />
    <br />
</p></td>
</tr>
<tr>
<td>Payment Option:</td>
<td>
  <label for="pay_option"></label>
  <select name="pay_option" id="pay_option">
    <option value="Counter">Counter</option>
    <option value="BDO">BDO</option>
    <option value="Smart Money">Smart Money</option>
  </select>
</td>
</tr>
<tr>
<td>Claiming Option:</td>
<td>
  <label for="claim_option"></label>
  <select name="claim_option" id="claim_option">
    <option value="Pick-up">Pick-up</option>
    <option value="Shipping">Shipping</option>
  </select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="button" id="button" value="Submit" /></td>
</tr>
    </table>
    </form>

基本上,无论用户选择什么,都应在单击提交按钮后插入到数据库列中。我不知道 it.I 的 PHP 脚本使用 Dreamweaver 和 mysql workbench

PHP

你给我的代码在最底部。其余的是我插入记录时由DW生成的 gui

<?php require_once('Connections/MCC.php'); ?>

<?php
if (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
case "long":
case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
$insertSQL = sprintf("INSERT INTO order_form (name, address, contact_no, payment_option, claim_option, orders) VALUES (%s, %s, %s, %s, %s, %s)",
                   GetSQLValueString($_POST['name'], "text"),
                   GetSQLValueString($_POST['address'], "text"),
                   GetSQLValueString($_POST['contactno'], "text"),
                   GetSQLValueString($_POST['pay_option'], "text"),
                   GetSQLValueString($_POST['claim_option'], "text"),
                   GetSQLValueString(isset($_POST['CheckboxGroup[]']) ? "true" : "", "defined","'Y'","'N'"));

mysql_select_db($database_MCC, $MCC);
$Result1 = mysql_query($insertSQL, $MCC) or die(mysql_error());
}

//-------added this---------
if (isset($_POST['submit']))
  {
    $implode=implode(",",$_POST['CheckboxGroup']);
    mysql_query("INSERT INTO order_form (orders) VALUES ('".$implode."')") ;
  }
?>

您可以通过以下两种方式存储值

1) 您可以序列化数组并将其存储在 table 的一列中(例如订单或产品)。 类似的东西

$orders = serialize($_POST['CheckboxGroup']);

insert into orders (products)values('".$orders."');

2) 或者您可以采用将创建重复的第二个选项。

foreach($_POST['CheckboxGroup'] as $val){
insert into orders (products)values('".$val."');
}

注意:这只是一个例子,向您展示 logic.you 可以根据您的要求制作。

试试这个

$implode=implode(",",$_POST['CheckboxGroup']);
insert into orders (products) values('".$implode."')

试试这个

<?php
if (isset($_POST['button']))
{
$implode=implode(",",$_POST['CheckboxGroup']);
mysql_query("INSERT INTO order_form (orders) VALUES ('".$implode."')") ;
}
?>

 <?php
 require_once('Connections/MCC.php');
 if(isset($_POST['button']))
 {
 $implode=implode(",",$_POST['CheckboxGroup']);
 mysql_query("INSERT INTO order_form (name, address, contact_no, payment_option,  claim_option, orders) VALUES ('".$_POST['name']."', '".$_POST['address']."',  '".$_POST['contact_no']."', '".$_POST['payment_option']."', '".$_POST['claim_option']."',  '".$implode"')");
}
?>