【问题标题】:unable to compare data from database to array of strings无法将数据库中的数据与字符串数组进行比较
【发布时间】:2024-05-29 20:10:01
【问题描述】:

我有一个程序,我需要尽可能多地插入单词,然后将通过数据库检查这些单词,如果它存在于数据库中,它应该返回数据库中存在的单词数。 请告诉我这段代码有什么问题,它没有将相似条目的数量返回到数据库

<html>
<head>

<script language="javascript" type="text/javascript">
// Pre-defined text:
var newtext = "This is the new text";
// Drop-Menu:
var newtext = myform.mymenu.options[myform.mymenu.selectedIndex].value;
// Prompt:
var newtext = prompt('Enter New Text Here:', '');
function addtext() {
    var newtext = document.myform.inputtext.value;
    document.myform.outputtext.value += newtext+'&nbsp;';
}

</script>
</head>
<body>
<form name="myform" action="" method="post">
<table border="0" cellspacing="0" cellpadding="5"><tr>
<td><textarea name="inputtext"></textarea></td>
<input type="radio" name="placement" value="append" checked> Add to Existing Text<br>
<td><p><input type="radio" name="placement" value="replace"> Replace Existing Text<br>
<input type="button" value="Add New Text" onClick="addtext();"></p>
</td>
<td><textarea name="outputtext"></textarea></td>
</tr></table>
<input type="submit"/>
</form>
<?php
$string=$_POST['outputtext'];
$array=array();
$array=explode(';',$string);
@ $db=new mysqli('localhost','root','','words');
if(mysqli_connect_errno())
{
    echo 'Error:Could not connect to the database';
}
else
echo 'connected';
$db->select_db('words');
$count = 0;
foreach($array as $s)
{    
    $query="select * from collection where word LIKE '%".$s."%'";
    $result=$db->query($query);
    if($result)
    $count += $db->num_rows;    
}
echo $count;
$db->close();
?>
</body>
</html>

【问题讨论】:

  • 您没有在查询中使用$s
  • 它没有显示我在 textarea 中输入了多少单词已经在数据库中。
  • $array 存储输入的单词。
  • 你有一个很大的安全漏洞(SQL 注入)。在查询中连接之前,您必须检查 POST vars(使用 real_escape_string),或者(更好地)使用 PDO 中提供的占位符。

标签: javascript php html mysql arrays


【解决方案1】:

$db-&gt;num_rows已经是行数了……不用手动统计了。

【讨论】:

    【解决方案2】:
    $count = 0;
    foreach($array as $s)
    {    
        $query="select count(*) as num_matched from collection where word LIKE '%".$s."%'";
        $result=$db->query($query) or die($db->error);
        $row = $result->fetch_assoc();
        $count += $row['num_matched'];
    }
    echo $count;
    

    您还应该切换到参数化查询,而不是直接使用输入。

    $stmt = $db->prepare("select count(*)
                          FROM collection
                          WHERE word LIKE CONCAT('%', ?, '%')");
    $stmt->bind_param("s", $s);
    $count = 0;
    foreach ($array as $s) {
        $result = $stmt->execute();
        $stmt->bind_result($num_matched);
        $stmt->fetch();
        $count += $num_matched;
    }
    echo $count;
    

    【讨论】:

    • 它返回我在数据库中的条目数而不是匹配的条目。
    • 不应该。它应该只计算匹配的条目。尝试打印 $query 并确保它包含您在 LIKE 子句中所期望的内容。
    【解决方案3】:

    在for循环中运行查询是不可行的,你可以试试下面的解决方案,

    $array=array('abc','xyz','lmn','pqr');     
    $query="select word from collection where word LIKE '%".$s."%'";
    $result=$db->query($query);
    while ( $row = mysql_fetch_array($result) )
    {
      $tblarray[] =  $row['word'];
    }
    foreach($tblarray as $k => $v)
    {
      foreach($array AS $key => $value)
       {
        if (strpos($v, $value) !== false)
         {
          $finalarray[] = $v; 
         }
      }
    }
    echo sum($finalarray);
    

    【讨论】:

      最近更新 更多