php - 插入多个表单选择到Mysql

标签 php mysql

需要 PHP Mysql 代码的帮助才能将多个选择数据插入数据库。使用当前代码,我在 MySQL 数据库的 ccaid 字段中只得到“Arry”,它应该包含第一个代码生成的 Md5 代码。

这是多选数据的代码:

<?php   include ('incl/_dbconnection.php');
        include ('incl/_dbopen.php');
        include ('_header.php'); 

        $base1 = '<option value="';
        $base2 = '">';
        $base3 = '</option>';

$updatekey = md5(uniqid(mt_rand(), true));
$clientId = md5(uniqid(mt_rand(), true));

$subject = 'Complete your Registration';

mysql_query("INSERT INTO clients (clientid, cname, caddress, ctel, cfax, cmobile, cemail, cperson, cdescription, cproducts, cupdatekey, cwebsite, clastip, cvalidated)
VALUES ('$clientId', '$_POST[company]', '$_POST[address]', '$_POST[tel]', '$_POST[fax]', '$_POST[mobile]', '$_POST[email]', '$_POST[person]', '$_POST[description]', '$_POST[products]', '$updatekey', '$_POST[website]', '$_POST[ipaddress]', '$_POST[validate]')");

?>
<div class="full-width-wrapper">
                <div class="fixed-width-wrapper" id="body-content">
                    <div id="content" class="float-left content-left">
                            <h1 class="first-word sp">
                                <strong>Client Registered!</strong>
                            </h1>
                        <div id="mlogin" class="clear">
                            <h2 align="center"><strong>Choose Categories!</strong></h2>
                            <br/>
                            <table width="100%" border="0" class="m-table">
<thead>
  <tr>
    <td colspan="2" scope="col">
                        <div class="formRight">
                            <form action="registration_complete.php?id=<?php echo $clientId ;?>" method="post">
                            <select multiple="multiple" class="multiple" title="" name="categories[]">
                            <?php $SQL = "SELECT * FROM wdcategories ORDER BY `caname` ASC";
    $result = mysql_query($SQL);

    while ($db_field = mysql_fetch_assoc($result)) {

    echo $base1;
    print $db_field['caid'];
    echo $base2;
    print $db_field['caname'];
    echo $base3;

    };?>
                            </select>
                            <br/><br/><br/>
                            <button type="submit" class="black border-radius-3px bold submit" title="">
                                    Complete Registration
                                </button>
                            </tr>
  </thead>
</table></form>
                            <div class="clear">
                            </div>
                        </div>
                    </div>
<?php   
        include ('_footer.php'); 
        include ('incl/_dbclose.php');
?>

下面是将多选数据插入数据库的代码:

<?php   include ('incl/_dbconnection.php');
        include ('incl/_dbopen.php');
        include ('_header.php'); 

        $id=$_GET['id'];

        $categories = $_POST['categories'];
for ($i = 0; $i < count($categories); $i++) {
    $category = $categories[$i];
    mysql_query("INSERT INTO ccategories (ccaid, cclientid) VALUES ('" . $_POST['categories'] . "','$id')");
} 

?>
<div class="full-width-wrapper">
                <div class="fixed-width-wrapper" id="body-content">
                    <div id="content" class="float-left content-left">
                            <h1 class="first-word sp">
                                <strong>Client Listed!</strong>
                            </h1>
                        <div id="mlogin" class="clear">
                            <h2 align="center"><strong>Registration Completed!</strong></h2>
                            <br/>
                            <img src="images/success.png" width="256" height="256">
                            <br/>
                            <h3 align="center"><a href="index.php">Click Here to add another Client</a></h3>
                            <div class="clear">
                            </div>
                        </div>
                    </div>
<?php 
        include ('_footer.php'); 
        include ('incl/_dbclose.php');
?>

最佳答案

mysql_query("INSERT INTO ccategories (ccaid, cclientid) VALUES ('" . $_POST['categories'] . "','$id')");

……应该是……

mysql_query("INSERT INTO ccategories (ccaid, cclientid) VALUES ('" . $category . "','$id')");

关于php - 插入多个表单选择到Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8479851/

相关文章:

php - 即使一个表没有结果,MySQL 是否可以生成一个查询以从多个表中获取?

php - 使用 JOIN 选择 3 个表但不能选择 4 个?

php - Mysql查询查杀脚本

javascript - 如何在 php 的表格中实现爆炸功能?

php - php require_once 文件路径中的变量

javascript - 返回函数 JavaScript

php - Laravel - 期权值(value)形式

php - 复杂 MySQL 查询的问题

mysql - MariaDB:未知数据类型 "MYSQL_JSON"

php - 仅显示已更改的列