简体   繁体   English

使用复选框从数据库中提取电子邮件地址(PHP,MYSQL)

[英]Pull Email Address From Database Using Checkboxes (PHP, MYSQL)

I'm a beginner with PHP/MYSQL and need a little help with a project I'm working on. 我是PHP / MYSQL的初学者,需要一些我正在进行的项目的帮助。

I'm building a contact database for a sales person. 我正在为销售人员建立一个联系人数据库。 She needs to be able to use a search term to look through her contacts for people who match that term. 她需要能够使用搜索词来查找与该词匹配的人的联系人。 When she searches a term and the list of matching contacts appears, she wants to be able to select certain entries using a checkbox and then get a list of their email addresses to copy and paste into Outlook so she can send them spreadsheets of goods she has for sale or other information. 当她搜索一个术语并且出现匹配联系人列表时,她希望能够使用复选框选择某些条目,然后获取他们的电子邮件地址列表以复制并粘贴到Outlook中,这样她就可以向她们发送她拥有的电子表格。出售或其他信息。

The search function (and everything else) is working perfectly, but I cannot seem to get this last part to work. 搜索功能(以及其他所有功能)完美运行,但我似乎无法让最后一部分工作。 Right now only the last person on the list's email address will display if that person is checked. 现在,如果选中该人员,则仅显示列表电子邮件地址中的最后一个人。 If the last person on the list is not checked, nothing will display no matter how many other people are checked. 如果未选中列表中的最后一个人,则无论检查了多少其他人,都不会显示任何内容。

I have searched here already and tried to implement the suggestions I have seen but I am not getting the results I want. 我已经在这里搜索并试图实施我所看到的建议,但我没有得到我想要的结果。 I'm hoping someone can help. 我希望有人可以提供帮助。 Thank you 谢谢

<? php
    if (isset($namesubmit)) {
        $sql = "SELECT * FROM contact WHERE name LIKE '%$searchName%'";
        $result = mysqli_query($connect, $sql);
        if (empty($searchName)) {
            echo '<span style="color:red">Please enter a name to search the database</span>';
        } elseif (mysqli_num_rows($result) > 0) {
            echo "<h3>Results For <b>{$searchName}</b>: <p></h3>";
            while($row = mysqli_fetch_assoc($result)) {
                $id = $row["contactID"];
                $email = $row["email"];
                $bizphone = $row['bizphone'];
                $mobphone = $row['mobphone'];
                echo '<h2>'  . $row["name"] . '<br /></h2>' . '<h3>' . $row["company"] . '<br />' . $row["website"] . '<br />' . "<a href=\"mailto:$email\">" . $email . '</a>' . '<br /> Business Phone Number: ' . "<a href=\"tel:+$bizphone\">" . $bizphone . '</a><br /> Mobile Phone Number: ' . "<a href=\"tel:+$mobphone\">" . $mobphone . '</a><br /> Business Address: ' . $row["address"] . '<br /> Buys: ' . $row["buys"] . '<br /> Sells: ' . $row["sells"]  . '<br />';
                echo  "<form method=\"POST\" action=\"updateform.php\"><input type=\"hidden\" name=\"sel_record\" value=\"$id\"><input type=\"submit\" name=\"update\" value=\"Edit Contact\"></form>" . '<p>' . "<form method=\"post\" action=\"deletecontact.php\"><input type=\"hidden\" name=\"sel_record\" value=\"$id\"><input type=\"submit\" name=\"delete\" value=\"Delete Contact\"></form><p><form method=\"POST\" action=\"emaillist.php\"><input type=\"checkbox\" name=\"action[]\" value=\"$email\" id=\"$email\">Send Email</h3><br /><hr />";
            }
            echo "<input type=\"submit\" name=\"massSubmit\" value=\"Send Email\"></form>";
?>

<? php
    $massSubmit = $_POST['massSubmit'];
    $action = $_POST['action'];
    if (isset($massSubmit)) {
        foreach ($action as $value) {
            echo $value;
        }
    }
?>

So right now it will only display the email address of the last person in the list if that person is checked. 所以现在它只会显示列表中最后一个人的电子邮件地址(如果已选中该人)。 If that person is not checked it won't display anything even if others on the list are checked. 如果未检查该人,即使检查了列表中的其他人,也不会显示任何内容。 Var_dump says NULL . Var_dump表示NULL

Thank you very much 非常感谢你

Edit: Thank you for the information about the nested forms. 编辑:感谢您提供有关嵌套表单的信息。 I deleted the other forms as a test and it is working now the way I wanted. 我删除了其他表格作为测试,它现在按照我想要的方式工作。 The problem now is how to integrate the ability to update the contact, delete the contact, and collect the email addresses all at the same time. 现在的问题是如何集成更新联系人,删除联系人和同时收集电子邮件地址的能力。 Can anyone help with this? 有人能帮忙吗?

If I understood your problem correctly, you want to select some options from the search results by checking some boxes and fetching additional data points. 如果我正确理解您的问题,您需要通过选中一些框并获取其他数据点从搜索结果中选择一些选项。 Here's a way I can see this being done: 这是我可以看到这样做的方式:

1) Make your checkbox inputs an array. 1)使您的复选框输入一个数组。 This way you can loop through them in your receiving PHP page: 这样你就可以在接收PHP页面中循环它们:

<input type="checkbox" name="salesperson[]" value="<?php echo $row["contactID"];?>>"

2) In your receiving PHP page, create a loop and form an array (let's call it $contactarray ) with all selected contactId entries. 2)在接收PHP页面中,创建一个循环并形成一个数组(让我们称之为$contactarray contactId ),其中包含所有选定的contactId条目。

3) Create a MySql query to fetch the additional data using a WHERE clause like below: 3)使用如下WHERE子句创建MySql查询以获取其他数据:

$query = "SELECT * FROM contact WHERE contactID in (" . $contactarray . ")";

4) Get the results of that query and you'll have all the info you need. 4)获取该查询的结果,您将获得所需的所有信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM