0

I'm a beginner with PHP/MYSQL and need a little help with a project I'm working on.

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.

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.

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?

Maths RkBala
  • 2,207
  • 3
  • 18
  • 21
elim6478
  • 1
  • 1
  • Hey best not to do nested forms like that - html wants separate forms which is why you have unexpected result – David Bray May 23 '19 at 21:24
  • Ok so what would be the best way to do what I need to do then? – elim6478 May 23 '19 at 21:34
  • Your code is wide open to [SQL injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – DarkBee May 24 '19 at 05:49
  • Please don't ask multiple questions in one question. If the comment above resolved your inital question please open a new question about updating the contacts – DarkBee May 24 '19 at 05:57
  • It is a good start, but doesn't answer the question because it removes the ability to update and delete contacts, which we need. I can't delete the other forms so it doesn't answer the question. – elim6478 May 24 '19 at 18:05

1 Answers1

0

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. This way you can loop through them in your receiving PHP page:

<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.

3) Create a MySql query to fetch the additional data using a WHERE clause like below:

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

4) Get the results of that query and you'll have all the info you need.

Paulo Hgo
  • 834
  • 1
  • 11
  • 26