php 7 pdo mysql regexp惠特变量进入查询

时间:2018-09-13 15:46:30

标签: php mysql regex variables pdo

我是PHP和mySQL的新手,我正在尝试使用这段代码来学习这些语言。

我有此代码:

<?php

class Cerca extends dbh{

  public function cercaArt(){
    $dbh = new dbh;
    $dbh->connect();

    if(isset($_POST['barcode']) || isset($_POST['codice']) || isset($_POST['descrizione'])){
      $barcode = $_POST['barcode'];
      $codice = $_POST['codice'];
      $descrizione = $_POST['descrizione'];

      $sql = "SELECT * FROM articoli WHERE barcode = ? REGEXP \"^[$barcode]\"  OR codice = ? REGEXP \"^[$codice]\"  OR descrizione = ? REGEXP \"^[$descrizione]\" ";
      $stmt = $this->connect()->prepare($sql);
      $stmt->execute([$barcode, $codice, $descrizione]);

      if($stmt->rowCount()>0){
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){

          echo "<tr>
                <td>". $row['codice'] ."</td>
                <td>". $row['descrizione'] ."</td>
                <td>". $row['prezzoA'] ."</td>
                <td>". $row['prezzoV'] ."</td>
                <td>". $row['quantita'] ."</td>
                <td>". $row['barcode'] ."</td>
                <td>". $row['esaurimento'] ."</td>
                <td>". $row['categoria'] ."</td>
                </tr>
                ";
          }
        }
    } else{
      echo 'Immissione non valida!';
      }
  }
}
?>

这是该脚本的形式代码:

    <?php
include 'includes/header.php';
session_start();
include 'includes/sidebar.php';
include 'includes/dbh.php';
include 'includes/cerca-articolo.php';
 ?>

   <!-- Header -->
<header class="w3-container" style="padding-top:22px">
  <h4><b><i class="fa fa-dashboard"></i> Anagrafica articoli</b></h4>
</header>

 <!-- CONTENT! -->

 <!-- FIRST ROW -->
  <div class="w3-row-padding w3-margin-bottom">
    <div class="w3-container w3-third">
      <form class="w3-container" action="anagrafica-articoli.php" method="post">
        <label class="w3-text-blue"><b>Barcode:</b></label>
        <input class="w3-input w3-border" name="barcode" type="text">
        <br>
        <label class="w3-text-blue"><b>Codice articolo:</b></label>
        <input class="w3-input w3-border" name="codice" type="text">
        <br>
        <label class="w3-text-blue"><b>Descrizione:</b></label>
        <input class="w3-input w3-border" name="descrizione" type="text">
        <br>
        <button class="w3-btn w3-blue" type="submit" value="submit">Cerca</button>
      </form>
    </div>
  </div>


<div class="w3-container">
  <table class="w3-table w3-striped">
    <tr>
      <th>Codice</th>
      <th>Descrizione</th>
      <th>Prezzo Acquisto</th>
      <th>Prezzo Vendita</th>
      <th>Quantità</th>
      <th>Barcode</th>
      <th>Esaurimento</th>
      <th>Categoria</th>
    </tr>
    <?php
      $search = new Cerca;
      $search->cercaArt();
     ?>
  </table>
</div>


 <?php
   include 'includes/footer.php';
  ?>

当我运行它时,出现此错误:

  

致命错误:未捕获的PDOException:SQLSTATE [42000]:语法错误或   访问冲突:1139得到错误“缺少终止字符”   在距regexp 3'处偏移的类   C:\ xampp \ htdocs \ bk \ includes \ cerca-articolo.php:16堆栈跟踪:#0   C:\ xampp \ htdocs \ bk \ includes \ cerca-articolo.php(16):   PDOStatement-> execute(数组)#1   C:\ xampp \ htdocs \ bk \ anagrafica-articoli.php(49):Cerca-> cercaArt()#2   {main}放在C:\ xampp \ htdocs \ bk \ includes \ cerca-articolo.php中   第16行

此查询用于搜索字段。此功能无需使用正则表达式,但如果我搜索的描述或条形码不完整,则不会显示数据。

如果描述为“ NIKE AIR SHOWS YELLOW”,而我搜索“ NIKE”,则表中未显示数据字段。 对于数据,我必须精确搜索“ NIKE AIR SHOWS YELLOW”字符串。

因此,我想尝试添加一个正则表达式来获取数据,例如,如果我仅搜索描述的一个单词,或者仅搜索一条条形码以显示所有文章都包含该条形码的内容,但是却出现该错误

0 个答案:

没有答案