PHP Array into mysql select Statement

时间:2020-12-20 04:28:35

I have an issue where i have a textarea in a form where users can enter names on seperate lines. On submit I explode the "\n".

我有一个问题,我有一个textarea的形式,用户可以在单独的行上输入名称。提交时我会爆炸“\ n”。

I then want to pass the values from the array into an Select statement, but when i run the script it only returns one result (the last one) from the array..

然后我想将数组中的值传递给Select语句,但是当我运行脚本时,它只返回数组中的一个结果(最后一个)。

here is the code below.

这是下面的代码。

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>&nbsp:&nbsp</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 />';

If you can help i would be greatful

如果你能帮助我会很棒

Thanks

2 个解决方案

#1


1  

Guessing you've got OS specific line endings tripping you up. Try preg_split

猜测你有特定于操作系统的行结尾绊倒了你。试试preg_split

$lines = preg_split("/\\n|\\r|\\r\\n/", $str);

#2


3  

Try using equals instead of IN:

尝试使用equals而不是IN:

$result = mysql_query("SELECT * FROM emails WHERE Name='$array_element' ORDER BY LastName");

$ result = mysql_query(“SELECT * FROM emails WHERE Name ='$ array_element'ORDER BY LastName”);

Otherwise my advise to debug it would be to put an exit(); statement on your first pass of the while loop to check your array value and/or if you get a result of the first entry. something like:

否则我建议调试它将是一个exit();第一次传递while循环时的语句,用于检查数组值和/或是否得到第一个条目的结果。就像是:

foreach($lines as $array_element) {
    $result = mysql_query("SELECT * FROM `emails` WHERE `Name`='$array_element' ORDER BY `LastName`");

    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    print_r($row);
    exit;
    ...

#1


1  

Guessing you've got OS specific line endings tripping you up. Try preg_split

猜测你有特定于操作系统的行结尾绊倒了你。试试preg_split

$lines = preg_split("/\\n|\\r|\\r\\n/", $str);

#2


3  

Try using equals instead of IN:

尝试使用equals而不是IN:

$result = mysql_query("SELECT * FROM emails WHERE Name='$array_element' ORDER BY LastName");

$ result = mysql_query(“SELECT * FROM emails WHERE Name ='$ array_element'ORDER BY LastName”);

Otherwise my advise to debug it would be to put an exit(); statement on your first pass of the while loop to check your array value and/or if you get a result of the first entry. something like:

否则我建议调试它将是一个exit();第一次传递while循环时的语句,用于检查数组值和/或是否得到第一个条目的结果。就像是:

foreach($lines as $array_element) {
    $result = mysql_query("SELECT * FROM `emails` WHERE `Name`='$array_element' ORDER BY `LastName`");

    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    print_r($row);
    exit;
    ...