【问题标题】:Multi-field form to sql method post request [closed]多字段表单到sql方法发布请求[关闭]
【发布时间】:2021-09-05 11:11:48
【问题描述】:

我的 html 页面包含 5 个字段。他们不一定都被告知。 方法 post 将字段的内容发送到 php 页面。 查询只能从数据库中提取与填写的字段相关的行。 html 页面发送请求,但数据库不返回任何内容。我不知道问题出在 html 端还是 php 页面上。 我挡在了复杂的整体面前。

主页:

<tr>
    <td style="font-family: 'Conv_Muli-Italic'; font-size: 16px; text-align: center; font-weight: bold; color: #808080;">
        <form action = "ocr6.php" method = "POST">
        <p>&nbsp;</p>
        <table width="100%">
            <tbody>
            <tr>
                <th scope="col">TYPE THE WORD OF YOUR CHOICE IN TEXT FIELDS. </th>
            </tr>
            </tbody>
        </table>
        <p>&nbsp;</p>
        <table width="100%" align="center">
            <tbody>
            <tr>
                <th scope="col"><p>NAME</p>
                    <p>
                        <input name="nom" type="text"  />
                    </p></th>
                <th scope="col"><p>STATE</p>
                    <p>
                        <input name="nom2" type="text" />
                    </p></th>
                <th scope="col"><p>CATEGORY</p>
                    <p>
                        <input name="nom3" type="text" />
                    </p></th>
                <th scope="col"><p>DETAIL</p>
                    <p>
                        <input name="nom4" type="text" />
                    </p></th>
                <th scope="col"><p>ORIGINE</p>
                    <p>
                        <input name="nom5" type="text" />
                    </p></th>
            </tr>

ocr6.php:

    <?php
    $mysqli = new mysqli("localhost", "....", "mot de passe", "mabase");
    
    if ($mysqli->connect_errno) {
      die('<p>Connexion impossible : '.$mysqli->connect_error.'</p>');
    }
    $nom = $_POST['nom'];
    $nom2 = $_POST['nom2'];         
    $nom3 = $_POST['nom3'];
    $nom4 = $_POST['nom4'];                 
    $nom5 = $_POST['nom5'];
        
    $stmt = $mysqli->prepare("SELECT FROM fiveC2 WHERE name LIKE ? OR state LIKE ? OR category LIKE ? OR detail LIKE ? OR origine LIKE ? LIMIT 0, 3000");
    $stmt->bind_param("sssss", '%$nom%', '%$nom2%', '%$nom3%', '%$nom4%', '%$nom5%');
    $stmt->execute();
    $result = $stmt->get_result();
    
    if (!$result) {
      die('<p>ERREUR Requête invalide : '.$mysqli->error.'</p>');
    }
    
    while ($row = $result->fetch_assoc()) {
      $recherche = $row['recherche'] ;
      echo '<p>'.$recherche.'</p>'."\r\n" ;
    }
    
    $result->free() ;
    
    $mysqli->close() ;
    ?>  </p>
        </blockquote>
    </ul>
                      <p><strong> - END OF REQUEST - </strong></p>
                  </div>
              </div>
            </div>
        </div>
    </body>
    </html>

感谢您的帮助。

【问题讨论】:

  • 婆罗多,欢迎来到 SO。你什么都没问!你有什么问题?
  • Bharata 还考虑在这两个文件上关闭 html 标记。 ;)

标签: php html mysqli


【解决方案1】:

您可以将部分代码替换为变量,这样可以使代码更加灵活。

$stmt = $mysqli->prepare("SELECT FROM fiveC2 WHERE $where_string LIMIT 0, 3000");
                                                      ^^^
                                           string containing "name LIKE ?" etc 

$stmt->bind_param($type_string, ...$value_array );
                     ^^^              ^^^
       string containing "sss"         |__ array containing values with 
                                           variable-length argument list (the ...)

要创建这些变量,您必须循环 $_POST 并跳过所有空值。

使用input_names =&gt; column names 组合(请参阅下面的注释)和您将在语句中使用的变量创建一个数组。

$names = [
  'nom'  => 'name',
  'nom1' => 'state',
  'nom2' => 'category',
  'nom3' => 'detail',
  'nom4' => 'origine'
];
$where_array  = [];  //note this is an array!
$type_string  = '';  
$value_array  = [];

然后循环$_POST设置变量

foreach($names as $input_name => $column_name){
    // if empty: skip
  if( $_POST[ $input_name ] === '' ) continue;

    //add to $where_array: "name LIKE ?"
  $where_array[] =  $column_name.' LIKE ?';  

    //add to $type_string:  "s"
  $type_string.= 's';

    //add to $value_array: "%abcdef%"
  $query_value[] = '%'.$_POST[ $input_name ].'%';
}

  // Test if there was anything at all in $_POST
if($type_string == ''){
   ... oops, there was no input 
}

  // Convert the $where_array to $where_string using " OR " in between
$where_string = implode(' OR ', $where_array); 

现在您拥有所需的一切。显然foreach() 在创建查询和执行语句之前。

例如

$_POST : [ nom  => '',              
           nom2 => 'abcdef',        
           nom3 => '12',            
           nom4 => '',
           nom5 => 'Bharata'
         ]

$where_string : 'state LIKE ? OR category LIKE ? OR origine LIKE ?'
$type_string  : 'sss'
$value_array  : ['%abcdef%', '%12%', '%Bharata%']

注意 将表单名称中的输入元素与数据库中的列名称相对应要容易得多。然后,您可以直接使用来自$_POST 的键名及其值。如果您将来在表单中添加另一个搜索元素,它也可以防止出现问题。

foreach($_POST as $column_name => $value){
    if( $value === '' ) continue;
    ...
    $query_value[] = "%$value%";
    }

有用的阅读:variable-length argument lists

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-05
    • 2016-12-15
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多