php foreach循环用几个词搜索mysql表

标签 php mysql forms foreach while-loop

我有一个名为 foods 的 mysql 表,其中包含列“id、name、addinfo、picname、mg1cup、mg100g”。我有一个表格,用户可以在其中提交 1-20 个食物名称。 php 文件将提交的食物名称放入名为 $terms[] 的数组中。我需要在 sql 表中搜索所有术语,并为每个术语的所有列返回结果。

但是,结果只显示提交的第一个词,重复多少次输入(例如,如果输入两个词,第一个词在结果中输出两次 - 而不是第一个词结果,然后第二个词的结果)。

我不知道我做错了什么。这是我的代码(我还没有添加清理字符串的功能):

<?php

error_reporting(E_ALL);

ini_set('display_errors', '1');

//connect to the wordpress (bluehost)DB 

require_once '../../../wp-config.php';

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect to mysql');

$Db = mysql_select_db(DB_NAME, $link) or die('Could not select database.');   

//check to see if the search term has a value, matches "name" column in DB, and if so, put it into an array called terms
if (isset($_POST['terms'])) {     

    $terms = ($_POST['terms']);

        if (count($terms) > 0 ) { 

            $results=array();

            foreach($terms as $term) {

                $sql = 'SELECT * from `wpqr_foods` WHERE `name` like  "%'. $term .'%" ORDER BY name ASC';


                $Q1 = mysql_query($sql);

                if(mysql_num_rows($Q1)) {

                    while ($Res = mysql_fetch_assoc($Q1)) {

                        $results[] = $Res; 

                    }

                }

                //$results = mysql_query($sql);


                $sql = 'SELECT * from wpqr_foods WHERE name LIKE "%'. $term .'%" ORDER BY name ASC';

                $Q2 = mysql_query($sql);

                //get results

                if(mysql_num_rows($Q2)) {

                    while ($Res = mysql_fetch_assoc($Q2)) {

                        $results[] = $Res; 

                    }

                }

            }

            if (count($results) > 0 ) {

                foreach ($results as $CurRes ) {

                    echo $CurRes['name']. "<br/>"; 
                    echo $CurRes['addinfo']. "<hr/>"; 

                    /* 

                    [id] => 5
                    [name] => Apples
                    [value] => Yes
                    [addinfo] => They can eat apples.  They can also eat the skin of the apple and the leaves of the apple tree, but the tree leaves are high in calcium, so limit their intake.  They can also chew on the apple tree branches.
                    [picname] => apple
                    [mgc1cup] => 5.8
                    [mgc100g] => 4.6

                    */
                }

            }

            else {

                echo "(nothing entered)";//MEANS $TERM IS EMPTY

            }


        }
        else {

            echo "(nothing entered)";//means $_POST IS NOT SET (end of if(isset($_POST['term'])) { )

        }             
}

//function to sanitize array values from form
function sanitizeString($var) {
         if (is_array($val))
           {
           foreach ($val as $k => $v)
               {
               $val[$k] = htmlentities(strip_tags($v),ENT_QUOTES);
               }
           }
         else
           {
             $val = htmlentities(strip_tags($terms),ENT_QUOTES);
           }
        return $val;
}
?>

HTML 是

<div class="my-form">
        <form role="form" id="step1" method="post" action="/wp-content/themes/mia/vitcdata.php">
            <p class="text-box"><label for="box1">Food <span class="box-number">1&nbsp;</span></label><input type="text" name="terms[]" value="" placeholder="apples" id="box1" />&nbsp;<a class="add-box" href="#">Add More</a></p>
            <p><input type="submit" value="Submit" /></p>
        </form>

该表单具有用户可以通过 javascript 添加的动态表单字段,他们最多可以添加 20 个文本输入,其中所有输入的术语都会添加到 $terms[] 数组中。

所以,如果我测试它,使用两个字段,并输入“apples”和“bananas”,结果显示“apples”的所有数据重复两次。

最佳答案

检查这个,你可以在这里检查一个相关的问题: Using OR in LIKE Query in MySQL to compare multiple fields

您可能必须在查询中执行类似的操作。

$sql = 'SELECT * from wpqr_foods WHERE name LIKE "%'. $term .'%" OR addinfo LIKE "%'. $term .'%" ORDER BY name ASC';

或者,

$sql = 'SELECT * FROM `wpqr_foods` WHERE CONCAT(name, addinfo ) LIKE "%'. $term .'%" ORDER BY name ASC';

关于php foreach循环用几个词搜索mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27641779/

相关文章:

php - Codeigniter 与其他非数据库功能的事务?

php - 如果存在页面重定向,成功函数中的 jQuery ajax 函数将被忽略

mysql - 对查询返回的列进行排序

java - 为什么 DateTime 字段上的 getTimestamp 返回的结果与选择 unix_timestamp 的结果不同?

forms - perl WWW::Mechanize Submit_form() 直接写入文件?

javascript - 带有向上/向下箭头的单行选择框,类似于多行选择框

php - Yii 框架不能在 CMap.php 中使用标量值作为数组

php - 在 PHP 中使用单例

php - 转换为 MyISAM, `foreign_key_checks = 0` 不工作

jquery - .serialize() 不适用于 Opera 中的特定形式