2个ifs,具有2个不同的返回插入查询

时间:2018-01-17 04:54:02

标签: php mysql

这是从表单提交数据的代码。它曾经很好地工作,直到只有第一个'如果'。现在我有2个多选列表菜单,数据通过相同的查询转到2个不同的列。

提交表单后,我收到一条消息“查询为空”

<html>

</head>
<body>
<?php
    $username="username";
    $password="password";
    $database="databasename";


    // Configuration Settings

    $momdate=$_POST["momdate"];
    $Committee=$_POST["Committee"];
    $Number=$_POST["Number"];
    $CommitteeName=$_POST["CommitteeName"];
    $momdate2=$_POST["momdate2"];
    $meeting_venue=$_POST["meeting_venue"];
    $member1=$_POST["member1"]; - multiple select option
    $member2=$_POST["member2"]; - multiple select option
    $momdate3=$_POST["momdate3"];
    $actiontakenpoints=$_POST["actiontakenpoints"];
    $items=$_POST["items"];
    $prepby=$_POST["prepby"];
    $approvedby=$_POST["approvedby"];

    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");


    if ($_POST) {
        $member1_string = implode(', ', $_POST['member1']); 
    } elseif ($_POST) {
        $member2_string = implode(', ', $_POST['member2']); 
    } else
        $query = "INSERT INTO `mangeshk_ktweb`.`dgmcmom` (`momdate`, `Committee`, `Number`, `CommitteeName`, `momdate2`, `meeting_venue`, `member1`, `member1`, `momdate3`, `actiontakenpoints`, `items`, `prepby`, `approvedby`) VALUES ('$momdate', '$Committee', '$Number', '$CommitteeName', '$momdate', '$meeting_venue', '$member1', '$member2', '$momdate3', '$actiontakenpoints', '$items', '$prepby', '$approvedby')";

    mysql_query($query) OR die(mysql_error()); 

    mysql_close();
?>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

尝试(未经测试):

if (empty($_POST['member1']) {
    $member1 = '';
} else {
    $member1 = $_POST['member1'];
}

然后对于成员2来说同样的事情。

答案 1 :(得分:0)

您可以使用If而不是if,因为member1和member2都具有值&#34; member2&#34;在数组格式中,您可以尝试使用以下代码。

$member1_string = $member2_string = '';
if (!empty($_POST['member1'])) {
    $member1_string = implode(', ', $_POST['member1']); 
    } 
if (!empty($_POST['member2'])){
    $member2_string = implode(', ', $_POST['member2']); 
    }