【问题标题】:PHP/MySQL - HTML Form Text Area Query (query each line in form) [closed]PHP/MySQL - HTML 表单文本区域查询(查询表单中的每一行)[关闭]
【发布时间】:2016-03-01 08:00:40
【问题描述】:

我需要一种方法让用户将数据粘贴到表单中的文本区域中 并单独查询每一行。

例如 - 假设用户需要获取每个人的年龄 从分贝。而不是一次做一个名字 - 用户粘贴 10 个名字。

名称1
名称2
name3 等

然后用户提交表单,php输出为:

名称1 | 43
名称2 | 21
名称3 | 17 等

有什么建议吗?

谢谢


<?php

//dbstuff:


$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$list = explode("\r\n", htmlentities($_POST['name'])); // 'names' is the name of your textarea; I use htmlentities to help sanitize the data; $list is now an array of the entries split by a new line character
foreach($list as $l){
    $name = $l;
    //query the DB
    $query= "select peg_site_id, fa_code from cpm where peg_site_id='". $name."'";
}

//DEBUG
$debug = 1; //Select 1 for ON, or 2 for OFF
if ($debug == 1){
echo "<br /><br />START DEBUG<br />" . "*********************************<br />" . $query. "<br /><br />" . $count_query. "<br /><br />" . "*********************************<br />" . "END DEBUG<br />";
}
else {
echo "";
}
?>
<html>
<head>
<style type="text/css">
h4 {font-family: sans-serif}
p {font-family: courier}
p.sansserif {font-family: sans-serif}
</style>
</head>
<body>
<?php

// Print out result

$result= mysql_query($query);
$num_results = mysql_num_rows($result);

for ($i=0; $i <$num_results; $i++)
{

$row = mysql_fetch_array($result);
echo "<b>Customer Site ID:</b> ".$row['fa_code'] . "   <b>Site ID:</b> ".$row['peg_site_id'] . "</br>";


//echo "--------------------------------------------------------";
//echo "</br></br>";

}

?> 
</font>

【问题讨论】:

    标签: php html mysql forms


    【解决方案1】:

    基本上,您将获取表单输入并将其拆分为单独的实体,然后对每个实体运行查询。例如:

    $list = explode("\r\n", htmlentities($_POST['names'])); // 'names' is the name of your textarea; I use htmlentities to help sanitize the data; $list is now an array of the entries split by a new line character
    foreach($list as $l){
        $name = $l;
        //query the DB  
    }
    

    这就是您要寻找的要点吗?

    编辑 您只收到最后一个,因为您只对最后一个进行查询。你这样做的方法似乎有点奇怪。当我插入//query the DB 时,我的意思是,此时查询数据库;不创建稍后运行的查询语句。你在做什么使用

    $query= "select peg_site_id, fa_code from cpm where peg_site_id='". $name."'";
    

    每次循环时都会重新创建变量$query。所以$query 变量将永远只有一个条目。 $num_results 永远只有一个。我会建议这样的事情:

    $html = '';
    foreach($list as $l){
        $name = $l;
        //query the DB
        $query= "select peg_site_id, fa_code from cpm where peg_site_id='".     $name."'";
        $result= mysql_query($query);
        $row = mysql_fetch_array($result);
        $html .= "<b>Customer Site ID:</b> ".$row['fa_code'] . "   <b>Site ID:</b> ".$row['peg_site_id'] . "</br>";
    }
    echo $html;
    

    您会注意到我创建了$html 变量,将查询结果与我想要的HTML 格式相结合,然后我将echo 一次性全部输出。希望对您有所帮助!

    附:作为旁注,我使用PDO 来查询数据库,因为mysql_ 已被弃用。我强烈建议改用mysqli_PDO 方法而不是mysql_ 方法。

    【讨论】:

    • 谢谢...这就是我得到的。我只看到查询中显示的最后一行。开始调试 ********************************* 选择姓名,年龄从 x where name='bob smith' ** ******************************* END DEBUG 结果:年龄:42 姓名:Bob Smith
    • 将我的代码添加到原始帖子中...谢谢...感谢任何帮助!
    • @user2680315 我已经根据您的编辑编辑了我的代码。请查看。
    • 做到了!非常感谢!
    • 只是一个后续问题...如果用户搜索“Bob”并且有多个“Bob”行怎么办。现在使用代码 - 它只会输出最后一行。我尝试了一个 while 循环 - 但我遗漏了一些东西......谢谢。
    【解决方案2】:

    您可以使用 PHP 中的 explode 函数将字符串从 textarea 中拆分出来。拆分字符串后,您将迭代结果数组并查询数据库。请参阅 explode 文档:http://php.net/manual/en/function.explode.php

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多