Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 选中复选框后,将触发更多事件_Php_Html - Fatal编程技术网

Php 选中复选框后,将触发更多事件

Php 选中复选框后,将触发更多事件,php,html,Php,Html,我正在制作一个应用程序,根据按下的复选框向数据库中添加一个名称和一些文本。 我有一个if语句,当两个复选框都被按下时触发。问题是,每次您同时选择这两个复选框时,它都会为第一个复选框、第二个复选框添加文本,然后将其合并。我希望,如果您同时按下这两个键,只显示组合文本。 这是有问题的代码 { if (isset($_POST['laptopCheck'])) { $pdo = new PDO("mysql:host=localhost;dbn

我正在制作一个应用程序,根据按下的复选框向数据库中添加一个名称和一些文本。 我有一个if语句,当两个复选框都被按下时触发。问题是,每次您同时选择这两个复选框时,它都会为第一个复选框、第二个复选框添加文本,然后将其合并。我希望,如果您同时按下这两个键,只显示组合文本。 这是有问题的代码

      {
        if (isset($_POST['laptopCheck'])) {
          $pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
          $nume = $_POST['nume'];
          $prenume = $_POST['prenume'];
          $obiecte = 'laptop';
          $q = "insert into users(nume,prenume,inventar)values(:nume,:prenume,:obiecte)";
          $r = $pdo->prepare($q);
          $r->execute(array(":nume"=>$nume, ":prenume"=>$prenume, ":obiecte"=>$obiecte));
        }
        if (isset($_POST['telefonCheck'])) {
          $pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
          $nume = $_POST['nume'];
          $prenume = $_POST['prenume'];
          $obiecte = 'telefon';
          $q = "insert into users(nume,prenume,inventar)values(:nume,:prenume,:obiecte)";
          $r = $pdo->prepare($q);
          $r->execute(array(":nume"=>$nume, ":prenume"=>$prenume, ":obiecte"=>$obiecte));
        }
        if (isset($_POST['telefonCheck']) && isset($_POST['laptopCheck'])) {
          $pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
          $nume = $_POST['nume'];
          $prenume = $_POST['prenume'];
          $obiecte = 'laptop si telefon';
          $q = "insert into users(nume,prenume,inventar)values(:nume,:prenume,:obiecte)";
          $r = $pdo->prepare($q);
          $r->execute(array(":nume"=>$nume, ":prenume"=>$prenume, ":obiecte"=>$obiecte));
        }
      }
这是我的全部代码:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form action="index.php" method="post">
      <input type="text" name="nume" placeholder="nume" required>
      <br>
      <input type="text" name="prenume" placeholder="prenume" required>
      <br>
      Laptop: <input type="checkbox" name="laptopCheck">
      <br>
      Telefon: <input type="checkbox" name="telefonCheck">
      <br>
      <input type="submit" name="sub" value="Adauga in baza de date">
      <br>
      <br>
      <br>
    </form>
    <table border="1px">
      <tr>
        <th>ID</th>
        <th>Nume</th>
        <th>Prenume</th>
        <th>Obiecte</th>
      </tr>
    <?php
      if(isset($_POST['sub']))
      {
        if (isset($_POST['laptopCheck'])) {
          $pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
          $nume = $_POST['nume'];
          $prenume = $_POST['prenume'];
          $obiecte = 'laptop';
          $q = "insert into users(nume,prenume,inventar)values(:nume,:prenume,:obiecte)";
          $r = $pdo->prepare($q);
          $r->execute(array(":nume"=>$nume, ":prenume"=>$prenume, ":obiecte"=>$obiecte));
        }
        if (isset($_POST['telefonCheck'])) {
          $pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
          $nume = $_POST['nume'];
          $prenume = $_POST['prenume'];
          $obiecte = 'telefon';
          $q = "insert into users(nume,prenume,inventar)values(:nume,:prenume,:obiecte)";
          $r = $pdo->prepare($q);
          $r->execute(array(":nume"=>$nume, ":prenume"=>$prenume, ":obiecte"=>$obiecte));
        }
        if (isset($_POST['telefonCheck']) && isset($_POST['laptopCheck'])) {
          $pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
          $nume = $_POST['nume'];
          $prenume = $_POST['prenume'];
          $obiecte = 'laptop si telefon';
          $q = "insert into users(nume,prenume,inventar)values(:nume,:prenume,:obiecte)";
          $r = $pdo->prepare($q);
          $r->execute(array(":nume"=>$nume, ":prenume"=>$prenume, ":obiecte"=>$obiecte));
        }
      }
      $pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
      $query = "select * from liamed.users";
      $d = $pdo->query($query);
      foreach ($d as $data){

    ?>

    <tr>
      <td><?php echo $data['id'];?></td>
      <td><?php echo $data['nume'];?></td>
      <td><?php echo $data['prenume'];?></td>
      <td><?php echo $data['inventar'];?></td>
    </tr>
  <?php }
  ?>
  </table>
  </body>
</html>



笔记本电脑:
电话:



身份证件 努姆 Prenume 目标

谢谢

只需更改检查值的顺序并使用elseif即可

您还可以删除相当多的重复代码

$pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");

$q = "insert into users
              (nume,prenume,inventar)
        values(:nume,:prenume,:obiecte)";
$r = $pdo->prepare($q);

$obiecte = false;

if (isset($_POST['telefonCheck']) && isset($_POST['laptopCheck'])) {
    $obiecte = 'laptop si telefon';
} elseif (isset($_POST['laptopCheck'])) {
    $obiecte = 'laptop';
} elseif (isset($_POST['telefonCheck'])) {
    $obiecte = 'telefon';
}

if ( $obiecte ) {  // only run if one of the above if's changed this from false
    $r->execute(array(":nume" => $_POST['nume'], 
                      ":prenume" => $_POST['prenume'], 
                      ":obiecte"=>$obiecte)
                );
}

您要执行~
if(telefoneCheck&&laptocheck){…}else if(telefoneCheck){…}else if(laptocheck){…}
。现在,当
telefoneCheck
laptocheck
为真时,它将输入所有3个条件,因为它们是3个独立的
if
语句。如果将其更改为
,否则如果更改为
,则它将只执行其中一个部分中的代码。您还需要将
电话和笔记本电脑
复选框移到第一个,否则它将过早地进入另一个部分,因为
如果(电话)
电话和笔记本电脑
都已设置时为真,只需使用
elseif
not
else,如果
@AlexBarker,您可以将其写成
elseif
else,如果
在PHP中都是有效且相同的。对此有轻微警告-如果
telefonCheck
laptocheck
都未提交,它仍将尝试插入一行,这与问题的逻辑略有不同。首先设置
$obecte=''
更安全,然后在执行查询之前,检查
是否($obecte!=''){$r->execute(…);}
。但是+1用于简化code@WOUNDEDStevenJones说得好,说得好。修正了我的想法