单个输入框的2个值,并将第二个值存储在php中


2 values for a single input box and storing the 2nd value in php

我在一个输入框中使用两个值,当用户选择多个框时,这些值将提交到submit_checkbox.php。我想在php表单中保存第二个值,即"|"符号后的值。我该怎么做?我的代码如下:-

<form name='checkbox' method="post" action="submit_checkbox.php">
    <b><h3>Select option</h3></b>
    <input type="hidden" name="qq1[]" value="null">
                <input type="checkbox" name="qq1[]" value="ABC|DEF"> A<br>
                <input type="checkbox" name="qq1[]" value="GHI|IJK"> B<br>
                <input type="checkbox" name="qq1[]" value="LMN|PQR"> C<br>
 <input type="submit" value="SUBMIT" >
    </form>

在"submit_checkbox.php"中,代码如下:-

    <?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'root1';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_select_db("my_db") or die(mysql_error());
    list($value1,$value2)=explode('|',$_POST['qq1']);
    $values=implode(',', $value2);
    $sql = "INSERT INTO print_chk VALUES ('$values')";    
$retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
    die('Could not enter data: ' . mysql_error());
    }
    else
    {
    echo "Success";
    }
    ?>

但是,我无法将第二个值放入sql表"print_chk"中。

您的$_POST['qq1']是一个数组,因此您必须分解其中的每个元素,然后从中生成字符串。试试这样的东西,它对我有效:

//Get the second part
function GetSecond($v) {
    if ($v != 'null') {
        $a = explode('|',$v);
        return $a[1];
    }
    else {
        return '';
    }
}
//Since $_POST['qq1'] is an array, get the second part of each item
$v = array_map('GetSecond',$_POST['qq1']);
//Filter out the empty strings
$v = array_filter($v);
//Implode the list
$values = implode(',',$v);
//Insert to DB
$sql = 'INSERT INTO print_chk VALUES ("'.mysql_real_escape_string($values).'")';