【发布时间】:2012-04-07 10:54:01
【问题描述】:
我有一个问题,我有一个文本区域,用户可以在其中单独输入名称。在提交时,我会分解“\n”。
然后我想将数组中的值传递到 Select 语句中,但是当我运行脚本时,它只返回数组中的一个结果(最后一个)..
这是下面的代码。
echo "<h1> You searched for the following names </h1>";
include 'conn.php';
mysql_select_db("email_finder", $con);
$Email = $_POST['EmailBox'];
$str = $Email;
$lines = explode("\n", $str);
//$in = implode(',', $lines);
//$userStr = implode(',', $lines);
echo "<table border='0'>
<tr>
<th style='color:White' width='180px'; bgcolor=#999999>Name</th>
<th style='color:White' width='250px'; bgcolor=#999999>Email</th>
</tr>";
echo "<pre>";
print_r($lines);
echo "</pre>";
foreach($lines as $array_element) {
$result = mysql_query("SELECT * FROM `emails` WHERE `Name` IN('$array_element') ORDER BY `LastName`");
echo "<pre>";
print_r($array_element);
echo "</pre>";
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo "<tr>";
echo "<td style='padding-left:5px'><b> ".$row['Name']."</b> : </td>";
printf("<td style='padding-left:5px'><a href=mailto:" .$row['Email']. ">" .$row['Email']. "</a></td>");
echo "</tr>";
echo "<pre>";
print_r($row);
echo "</pre>";
}
}
echo "</table><br />";
echo "Email <b>ALL</b> these Students: <a href=mailto:".$row['Email']." >Click Here</a> <br />";
mysql_close($con);
echo '<br />';
如果你能帮上忙,我会很高兴
谢谢
【问题讨论】:
-
您正在为数组中的每个元素运行一个查询,而不是一次获取所有行的查询。
-
也创建一个
$result的数组。 -
您的
$lines = explode("\n", $str);行可能会在每行末尾留下\r。也许试试$lines = preg_split("/\\r\\n|\\r|\\n", $str); -
@Matthieu mhm 没怎么用过这个系统——刚接触它
标签: php mysql arrays select textarea