【问题标题】:Dreamweaver multiple checkbox selection then insert to MySQLDreamweaver 多个复选框选择然后插入到 MySQL
【发布时间】:2015-01-02 17:47:29
【问题描述】:

我有一个订单,产品在复选框中。所选项目应插入到数据库列中。我在编码方面还不是很好,所以如果你给我看一个例子会很棒。谢谢。

这是我的桌子:

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

基本上,无论用户选择什么,都应该在单击提交按钮时插入到 db 列中。我不知道它的 PHP 脚本。我使用 dreamweaver 和 mysql 工作台

PHP

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

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

【问题讨论】:

  • 请拨打tour 并阅读How to Ask,了解我们对问题的期望。以目前的形式,您的问题无法回答,因为它太宽泛了。请收集更多信息,然后edit您的帖子。

标签: mysql database checkbox insert dreamweaver


【解决方案1】:

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

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

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

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

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

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

注意:这只是一个例子,向您展示逻辑。您可以根据自己的要求制作。

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

    • 仍然得到 N。我不知道了。也许我把代码放在了错误的位置。你知道mysql复选框的任何教程吗?
    • 等我给你输出
    • 请检查第 14 行的代码兄弟有什么错误...您遇到的问题是什么,我已经给出了解决方案,但您粘贴的代码有问题。
    • 解析错误:语法错误,第 14 行 C:\xampp\htdocs\MCC\orderform.php 中的意外 '"')"' (T_CONSTANT_ENCAPSED_STRING)
    • 检查双和单的问题本身是否存在
    【解决方案3】:

    试试这个

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

    【讨论】:

    • 我首先使用 Dreamweaver 的 gui 将插入记录用于表单中的其他数据,并将其插入到顶部。 if (isset($_POST['submit'])) { $implode=implode(",",$_POST['CheckboxGroup']); mysql_query("INSERT INTO order_form (orders) VALUES ('".$implode."')") ;它只在列中插入 N。不是所选项目。我做错了什么?
    • 检查一次订单字段的数据类型
    • 该字段的数据类型应该是什么?
    • type->varchar,length->255
    • 仍然得到 N.T___T
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2015-07-07
    • 1970-01-01
    相关资源
    最近更新 更多