简体   繁体   English

PDO准备语句和SQL LIKE多个值

[英]PDO Prepared Statement and SQL LIKE multiple values

I've got a search form where the user can search for a serial number or name of product. 我有一个搜索表,用户可以在其中搜索产品的序列号或名称。

Here's my code: 这是我的代码:

<?php
require_once "pdo_rothConn.php";

if (isset($_POST['searchText'])){

$sql = "SELECT m_ipdb, m_name FROM machine WHERE 'm_ipdb' LIKE :number OR 'm_name' LIKE :name";
$stmt = $dbh->prepare($sql);
$stmet->execute(array(
    ':number' => $_POST['searchText'],
    ':name' => $_POST['searchText']));

while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
 echo($row['m_ipdb']);
 echo($row['m_name']);
}
?>

<p>Search for a Machine</p>
 <form method="post">
 <table width="500" border="1">
 <tr>
  <td>Enter IPDB Number or Name:</td>
  <td><input name="searchText" type="text" id="searchText" /></td>
 </tr>
 <tr>
  <td colspan="2"><input type="submit" value="Search Database"></td>
  <td><a href="#">Cancel</a></p></td>
 </tr>
  </table>
  </form>
  </body>
  </html>

First, is there a more concise way I can write my SQL statement since the $_POST value I'm searching for is the same? 首先,由于要搜索的$ _POST值相同,因此我可以编写更简洁的SQL语句吗? I wasn't sure if there as an error in my SQL statement also with the multiple LIKE statements. 我不确定多个LIKE语句在我的SQL语句中是否也存在错误。

As of now the resulting page is coming up blank and not working at all. 到目前为止,结果页面空白,并且根本无法正常工作。 I thought of splitting the code into a page and processing page instead of a post-back page. 我想将代码分为页面和处理页面而不是回发页面。 I'm stuck. 我被卡住了。 But I asked this question before I learned about PDO and prepared statements and got feedback to learn about prepared/parameterized statements. 但是,在我了解PDO和准备好的语句并获得反馈以了解准备好的/参数化的语句之前,我问了这个问题。 Is the resulting code properly protecting against SQL injection as well? 生成的代码是否也可以正确地防止SQL注入?

Thanks. 谢谢。

$ stmet->执行到$ stmt->执行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM