douhao2011 2015-08-11 13:04
浏览 34
已采纳

如何使用通过HTML表单发送的变量显示mysql中的某些行

I'm trying to get certain data which meets the criteria from the database using AND condition with user searchable HTML form which sends the data to the search.

Code:

<?php
$conn = new mysqli('localhost', 'user', 'pass', 'db');
if ($conn->connect_error) die($conn->connect_error);
$conn->set_charset("utf8");

if (isset($_POST['Kohderyhmä']) &&
isset($_POST['Näytön aste']) &&
isset($_POST['Vaikutusten vahvuus']) &&
isset($_POST['Käyttökelpoisuus']) &&
isset($_POST['text']))
{
$Kohderyhmä = get_post($conn, 'Kohderyhmä');
$Näytön_aste = get_post($conn, 'Näytön aste');
$Vaikutusten_vahvuus = get_post($conn, 'Vaikutusten vahvuus');
$Käyttökelpoisuus = get_post($conn, 'Käyttökelpoisuus');
$text = get_post($conn, 'text');


    $query = "SELECT * FROM `tietokanta`
        WHERE Kohderyhmä='$Kohderyhmä' AND `Näytön aste`='$Näytön_aste' AND `Vaikutusten vahvuus`='$Vaikutusten_vahvuus' AND `Käyttökelpoisuus: luokka`='$Käyttökelpoisuus'";
         }

  $results = $conn->query($query);
if (!$results) die ("Database access failed: " . $conn->error);

$rows = $results->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$results->data_seek($j);
$row = $results->fetch_array(MYSQLI_ASSOC);

echo '<h3>' . $row['Nimi'] . '</h3><br />';
echo ''  . $row['Kokonaisarvio'] .   '<br />';
echo ''  . $row['Kuvaus'] .  '<br /><br />';

}
?>

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">

<b>Kohderyhmä</b><br />
<select name="Kohderyhmä" style="width: 150px;">
<option value="Kaikki">Kaikki</option>
<option value="Pikkulapset">Pikkulapset</option>
<option value="Alle kouluikäiset">Alle kouluikäiset</option>
<option value="Alakouluikäiset">Alakouluikäiset</option>
<option value="Nuoret">Nuoret</option>
<option value="Perheet">Perheet</option>
<option value="Vanhemmat">Vanhemmat</option>
<option value="Työntekijät">Työntekijät</option>
</select>
<br />

<b>Näytön aste</b>
<select name="Näytön aste" style="width: 150px;">
<option value="Kaikki">Kaikki</option>
<option value="Vahva">Vahva</option>
<option value="Kohtalainen">Kohtalainen</option>
<option value="Heikko">Heikko</option>
<option value="Ei riittävää näyttöä">Ei riittävää näyttöä</option>
<option value="Ei arvioitu">Ei arvioitu</option>
</select>
<br />

<b>Vaikutusten vahvuus</b>
<select name="Vaikutusten vahvuus" style="width: 150px;">
  <option value="Kaikki">Kaikki</option>
  <option value="Vahva">Vahva</option>
  <option value="Kohtalainen">Kohtalainen</option>
  <option value="Heikko">Heikko</option>
  <option value="Ei vaikutusta">Ei vaikutusta</option>
  <option value="Ei arvioitu">Ei arvioitu</option>
</select>
<br />

<b>Käyttökelpoisuus</b>
<select name="Käyttökelpoisuus" style="width: 150px;">
  <option value="Kaikki">Kaikki</option>
  <option value="Vahva">Vahva</option>
  <option value="Kohtalainen">Kohtalainen</option>
  <option value="Heikko">Heikko</option>
  <option value="Ei käyttökelpoinen">Ei käyttökelpoinen</option>
  <option value="Ei arvioitu">Ei arvioitu</option>
</select>
<br />
<br />
&nbsp;

Haku:        <input type="text" name="text" />
    &nbsp; &nbsp;
<input type="submit" value="Hae" />
    </form>

I haven't used PHP to contact database before so the PHP code is very messy. I don't understand any more than the very basics from PHP, I haven't used variables or objects or anything complex before.

HTML form: variable1 variable2 variable3 variable4 variable5 ---> PHP script: select * from db where variable1 and variable2 and variable3 and variable4 ---> display results matching the criteria

Current code causes this error message in error_log: PHP Warning: mysqli::query(): Empty query in /home/user/public_html/folder/script.php on line 23

I have already tried over 15 different variations of variables and sql query in total and nothing has worked..

  • 写回答

2条回答 默认 最新

  • dongyongju9560 2015-08-11 21:24
    关注

    If we shorten your if (isset($_POST ... something you can clearly see. This instruction

    $results = $conn->query($query);
    

    is always executed, regardless of whether isset returns true or not.

    if (isset($_POST['Kohderyhmä']) &&
     ...)
    {
      $Kohderyhmä = get_post($conn, 'Kohderyhmä');
      ...
      $query = "SELECT * FROM `tietokanta`...."
    }
    
    $results = $conn->query($query);
    

    So if only one field has not been filled out correctly, the error is always the same :
    PHP Warning: mysqli::query(): Empty query in ....
    This makes it difficult to determine where the fault really comes from.

    Place the curly bracket } behind database logic.

    if (isset($_POST['Kohderyhmä']) &&
     ...)
    {
      $Kohderyhmä = get_post($conn, 'Kohderyhmä');
      ...
      $query = "SELECT * FROM `tietokanta`...."
      $results = $conn->query($query);
      if (!$results) die ("Database access failed: " . $conn->error);
      $rows = $results->num_rows;
      for ($j = 0 ; $j < $rows ; ++$j)
       {
         $results->data_seek($j);
         $row = $results->fetch_array(MYSQLI_ASSOC);
         ....
       } 
    }
    ?>
    

    create a short test program to test only the database. Set only really necessary data fields in the query

    test.php

    <?php
    $conn = new mysqli('localhost', 'user', 'pass', 'db');
    if ($conn->connect_error) die($conn->connect_error);
    $conn->set_charset("utf8");
    
    $Kohderyhmä = "KohderyTest"; // replace with really existing values
    
    $query = "SELECT * FROM `tietokanta` WHERE Kohderyhmä='".$Kohderyhmä."' ";
    $results = $conn->query($query);
    if (!$results) die ("Database access failed: " . $conn->error);
    
      while ($row = $results->fetch_assoc()) {
        echo "<h3>" . $row['Nimi'] . "</h3><br />";
        echo $row['Kohderyhmä'] ."<br /><br />";
      }
    $results->free();
    ?>
    

    Add hardcoded variables $Näytön_aste = "reallyExistingValue"; , add query data field for data field and watch when it starts to stutter.


    Also we can not see your function get_post()

    If you mean the Wordpress function get_post(), your call to the function is wrong.

    I can well imagine that the failure from the function get_post() comes.
    And you always false or empty values assigns.

    $Kohderyhmä = get_post($conn, 'Kohderyhmä');
    

    assign it direct.

    $post = $_POST;
    if (isset($post['Kohderyhmä']) &&
    ...)
    {
    $Kohderyhmä = $post['Kohderyhmä'];
    ...
    

    Also you are using all select fields from the <form>, in the query.

    4 Select's with 8,6,6,6 options means

    8x6x6x6 == 1728

    1728 possibilities are you shure you have one datarecord where all values matches.

    WHERE Ko...='$Ko...' AND `Näy...`='$Näy...' AND `Vai...`='$Vai...' AND `Käy...`='$Käy...'";
    

    WHERE All four Datafields must match to get a result !!!!!!!!!!!!!

    You have to find a combination where all four values simultaneously exist.


    UPDATE

    OP new question :
    If you want empty or some named values stop searching for a value in database.

    required every single variable to be found in the database which it didn't find because I couldn't set the variable and there is no value for "Kaikki" in the database, the word "Kaikki" means all choices below that choice in the HTML form and for that I need some PHP

    Here comes the new test.php

    • 1) don't do $post['Näytön aste']; In the form the name is
      <select name="Näytön aste" style="...">.
      This will translated by submit to
      $post['Näytön_aste']; look at the underscore _
      This must be done with all select name with spaces in the name !!
    • 2) That was the reason why you get not all $_POST[....] values !
      OK ?
    • 3) replace in your form
      <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
      with
      <form action="testNew.php" method="POST">

    testNew.php


    <?php
    $conn = new mysqli('localhost', 'user', 'pass', 'db');
    if ($conn->connect_error) die($conn->connect_error);
    $conn->set_charset("utf8");
    $post = $_POST;
    if (isset($post['Kohderyhmä']) &&
        isset($post['Näytön_aste']) &&
        isset($post['Vaikutusten_vahvuus']) &&
        isset($post['Käyttökelpoisuus']))
     {
      $Kohderyhmä = $post['Kohderyhmä'];
      $Näytön_aste = $post['Näytön_aste'];
      $Vaikutusten_vahvuus = $post['Vaikutusten_vahvuus'];
      $Käyttökelpoisuus = $post['Käyttökelpoisuus'];
     } else { die ("No valid values"); }
        $count = 0;
        $and   = "";
        $query = "";
    
        if (!empty($Kohderyhmä) && $Kohderyhmä !="Kaikki" ) {
          if ($count > 0) { $and   = " AND ";  }
          $count++;
          $query = $query.$and."`Kohderyhmä`= '".$Kohderyhmä."'";
        }
        if (!empty($Näytön_aste) && $Näytön_aste !="Kaikki" ) {
          if ($count > 0) { $and   = " AND ";  }
          $count++;
          $query = $query.$and."`Näytön aste`= '".$Näytön_aste."'";
        }
        if (!empty($Vaikutusten_vahvuus) && $Vaikutusten_vahvuus !="Kaikki" ) {
          if ($count > 0) { $and   = " AND ";  }
          $count++;
          $query = $query.$and."`Vaikutusten vahvuus`= '".$Vaikutusten_vahvuus."'";
        }
        if (!empty($Käyttökelpoisuus) && $Käyttökelpoisuus !="Kaikki" ) {
          if ($count > 0) { $and   = " AND ";  }
          $count++;
          $query = $query.$and."`Käyttökelpoisuus: luokka`= '".$Käyttökelpoisuus."'";
        }
        if ($count > 0) {
            $query = "SELECT * FROM `tietokanta` WHERE ".$query;
            } else {
            $query = "SELECT * FROM `tietokanta`";    
            } 
        echo $query;
    
    if ($results = $conn->query($query)) {
    while ($row = $results->fetch_assoc()) {
        echo "<h3>" . $row['Nimi'] . "</h3><br />";
        echo $row['Kohderyhmä'] ."<br /><br />";
      }
    } else {
      echo "with your choices no records were found";
    }
    $results->free();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向