PHP错误-不知道如何修复它


php error - don't know how to fix it

我真的不擅长PHP。所以,是的,这段代码很简单。但如果可能的话,我宁愿让这个代码工作。谢谢你!

PHP代码的目的

从技术上讲,有两个领域,男性和女性性别。我试图触发一个声明,假设如果一个人为两个都选择0,告诉他们填充其中一个,否则,如果是空的,填充它,否则,通过底部的test_input函数来测试它。

我一直得到的是一个错误,始终这样说:

PHP Parse error: syntax error, unexpected '[', expecting ')'关于PHP代码的第一行。我把代码翻了这么多遍,但还是不知道如何修复它。

PHP代码

   if ((($_POST["male"]) === "") && ($_POST(["female"]) === "")) {
     $quantityErr = "pls fill in one of the genders";
   elseif (empty($_POST(["male"])) 
    $maleErr = "# of people (gender male) required";
   else 
     $male = test_input($_POST["male"]);
 }

HTML字段码

       <div class="field">
          <label>* Number of People</label>
          <select class="ui dropdown" name="male">
            <option value="">Gender Male</option>
            <option <?php if ($male === 0 ) echo 'selected' ; ?> value="0">0</option>
            <option <?php if ($male == 1 ) echo 'selected' ; ?> value="1">1</option>
            <option <?php if ($male == 2 ) echo 'selected' ; ?> value="2">2</option>
            <option <?php if ($male == 3 ) echo 'selected' ; ?> value="3">3</option>
            <option <?php if ($male == 4 ) echo 'selected' ; ?> value="4">4</option>
            <option <?php if ($male == 5 ) echo 'selected' ; ?> value="5">5</option>
            <option <?php if ($male == 6 ) echo 'selected' ; ?> value="6">6</option>
            <option <?php if ($male == 7 ) echo 'selected' ; ?> value="7">7</option>
            <option <?php if ($male == 8 ) echo 'selected' ; ?> value="8">8</option>
            <option <?php if ($male == 9 ) echo 'selected' ; ?> value="9">9</option>
            <option <?php if ($male == 10 ) echo 'selected' ; ?> value="10">10</option>
          </select>
          <?php if(isset($maleErr)) print ('<span class="error">* ' . $maleErr . '</span>'); ?>
          <?php if(isset($quantityErr)) print ('<span class="error">* ' . $quantityErr . '</span>'); ?>
        </div>

测试函数

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

This

$_POST(["female"])

访问POST阵列的female值不正确。

可使用花括号{}或括号[]访问数组。

在访问数组元素时,方括号和大括号可以互换使用(例如,在上面的例子中,$array[42]和$array{42}都可以做同样的事情)。

应该是正确的:

if (empty($_POST["male"])  && empty($_POST["female"])) {
     $quantityErr = "pls fill in one of the genders";
} elseif (!empty($_POST["male"]) && $_POST["male"] > 0) { 
     $male = test_input($_POST["male"]);
} elseif (empty($_POST["male"])) {
    //male is not set or equal to 0; if male is 0 then female >= 1 and set. logic doesn't have any female checks so unclear currently how you want to handle that
    $maleErr = "# of people (gender male) required"; // maybe add message that it is required to be greater than 1.
}

你也有不正确的条件。当在控制结构上使用{}时,您需要关闭控制块。你的elseif没有关闭之前的if

你写错了语法

试试这个

 if ((($_POST["male"]) === "") && ($_POST["female"] === "")) {
     $quantityErr = "pls fill in one of the genders";
   elseif(empty($_POST["male"]))
    $maleErr = "# of people (gender male) required";
   else 
     $male = test_input($_POST["male"]);
 }