-1

I have a form by which i use to send mails to users.

<form method="post" action="/functions/mails/mailstomysql.php">
<?php
$sql="SELECT UserId, FatherName, FirstName FROM profiles"; 

echo "<div class='FieldTitle' style='display:none;'>To</div>";
echo "
<div class='ui segment'>
  <div class='ui fluid multiple search selection dropdown'>
    <input type='hidden' multiple name='ReceiverId[]' required>
    <i class='dropdown icon'></i>
    <input class='search' tabindex='0'>
    <div class='default text'>To</div>
    <div class='menu' tabindex='-1'>

    "; // list box select command
foreach ($conn->query($sql) as $row){//Array or records stored in $row

echo "
<option class='item' data-value='".$row['UserId']."' value='".$row['UserId']."'>$row[FirstName] s/o $row[FatherName]</option>"; 

}
 echo "
</div>
</div> 
</div>
?>

    <input type="text" name="MailSubject">

    <textarea type="text" name="MailContent"></textarea>

    <input id="btnAddRecord" name="submit" type="submit" value="Send">  

</form>

This is the SQL Statement that retrieve the data for above form:

$sql="SELECT UserId, FirstName FROM profiles"; 

And this is the mailtomysql.php file which insert the data into MySQL database.

$ReceiverId=$_POST['ReceiverId'];
$MailSubject=$_POST['MailSubject'];
$MailContent=$_POST['MailContent'];

$sql = "INSERT INTO mails (
`ReceiverId`, `MailSubject`, `MailContent`, `MailRead`, `MailDate`
)
VALUES (
'$ReceiverId', '$MailSubject', '$MailContent', '1', CURRENT_TIMESTAMP()
)";

if ($conn->query($sql) === TRUE) {
echo "Mail sent!";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;

The above code works awesome and perfect.

My Question

The above mailtomysql.php file insert the selected users from Multiple Selection Box and post the selected user ids to ReceiverId column which i do not want that, instead i want to have row for every selected user.

like:

Now

ReceverId


114, 265, 112


What i want

ReceverId


114


256


112


Why

If a user want to delete the mail then it will delete the mail and other users which are in that mail will not see the mail too because it is deleted by a user.

So once more my question is how to make mailtomysql.php file to make row for every selected users rather than having selected users ids in one row.

Edited: I used the Semantic-Ui to select the options from the dropdown list, but it is not working.

qais ali
  • 77
  • 9
  • You have to use `ReceiverId` as an array. So you can loop on it. – Isukthar Oct 30 '16 at 22:17
  • Thanks for the comment can you write it down. :) – qais ali Oct 30 '16 at 22:18
  • you have a parsing error happening here, as does one of the answers below. You should ask them about it. Keywords: "error reporting". – Funk Forty Niner Oct 30 '16 at 22:59
  • `$row[FirstName` - parse error. – Funk Forty Niner Oct 30 '16 at 23:05
  • Is the `=== TRUE` thing strictly necessary? That seems like some serious Cargo Cult Programming there. – tadman Oct 31 '16 at 00:55
  • 1
    **WARNING**: This has some severe [SQL injection bugs](http://bobby-tables.com/) because `$_POST` data is used inside the query. Whenever possible use **prepared statements**. These are quite straightforward to do in [`mysqli`](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [PDO](http://php.net/manual/en/pdo.prepared-statements.php) where any user-supplied data is specified with a `?` or `:name` indicator that’s later populated using `bind_param` or `execute` depending on which one you’re using. – tadman Oct 31 '16 at 00:56
  • Now that doesn't look like a form... where is the select there? – barudo Nov 05 '16 at 11:48
  • Thanks for the -1 but I used the Semantic-Ui which work like select. Please do check for Semantic-Ui. – qais ali Nov 05 '16 at 11:50
  • get to the basic, use a select > option tag.... it is also available in semantic UI... http://semantic-ui.com/modules/dropdown.html#selection . Don't complicate yourself with using an input tag that is hidden.... – barudo Nov 06 '16 at 10:47
  • I have searched but couldn't found, if you can then do please do let me know and I will edit the question. – qais ali Nov 06 '16 at 12:49

2 Answers2

0

On form side:

<form method="post" action="/functions/mails/mailstomysql.php">

<select multiple name='ReceiverId[]'>
<?php foreach ($conn->query($sql) as $row){
    echo "<option class='item' data-value='".$row['UserId']."' value='".$row['UserId']."'>". $row['FirstName'] . "</option>";
} ?>
</select>

<input type="text" name="MailSubject">
<textarea type="text" name="MailContent"></textarea>

<input id="btnAddRecord" name="submit" type="submit" value="Send">  

Make sure the value is set in the option and the name of the select is ReceiverId[].

On the receiving end, this will result into only one query...

$ReceiverIds = $_POST['ReceiverId'];
$values = "";
foreach($ReceiverIds as $receiver){
    //don't forget some validation here...
    //this is potentially unsafe too since the values are not properly escaped. Perform proper escaping here...
    $values .= "('$receiver', '$MailSubject', '$MailContent', '1', CURRENT_TIMESTAMP()
    ),\n";
}
$values = rtrim($values, ",\n");
$sql = "INSERT INTO mails (
        `ReceiverId`, `MailSubject`, `MailContent`, `MailRead`, `MailDate`
    )
    VALUES
    $values";
$success = $conn->query($sql)
barudo
  • 665
  • 4
  • 13
  • Thanks for the answer, but i have this error `Warning: Invalid argument supplied for foreach() in G:\root1\data\localweb\functions\mails\mailstomysql.php on line 24` – qais ali Oct 30 '16 at 22:41
  • update the html to include [] in the name of the select. Use ReceiverId[] in place of ReceiverId. See http://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php – Jay Rajput Oct 30 '16 at 22:43
  • sorry for headache i use Semantic ui multiple select not option is it also applicable with that. – qais ali Oct 30 '16 at 22:57
  • `$row[FirstName']` that's a parse error right there, btw. – Funk Forty Niner Oct 30 '16 at 23:04
  • @barudo no luck still same. The user Ids are posted in one row in `ReceiverId` column. – qais ali Oct 30 '16 at 23:16
  • @barudo not woking – qais ali Oct 31 '16 at 20:07
  • Dear @barudo please do have a look to my form. I have changed the ` – qais ali Nov 04 '16 at 18:07
-1
foreach($ReceiverId as $item){
    $sql = "INSERT INTO mails (
    `ReceiverId`, `MailSubject`, `MailContent`, `MailRead`, `MailDate`
    )
    VALUES (
    '$item', '$MailSubject', '$MailContent', '1', CURRENT_TIMESTAMP()
    )";

    $success = $conn->query($sql)
}`
Isukthar
  • 185
  • 8
  • Thanks for the answer, but i have this error `Undefined variable: item in on line 22` and `Invalid argument supplied for foreach() on line 22` – qais ali Oct 30 '16 at 22:26
  • Code updated (swith receivedId and item). You can improve using multiple_query instead of query. – Isukthar Oct 30 '16 at 22:28
  • it says `Warning: Invalid argument supplied for foreach() in G:\root1\data\localweb\functions\mails\mailstomysql.php on line 22` – qais ali Oct 30 '16 at 22:30
  • Check all fields with isset before. – Isukthar Oct 30 '16 at 22:34
  • update the html to include [] in the name of the select. Use ReceiverId[] in place of ReceiverId. See http://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php – Jay Rajput Oct 30 '16 at 22:37
  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. **Flaggers / reviewers:** [For code-only answers such as this one, downvote, don't delete!](//meta.stackoverflow.com/a/260413/2747593) – Scott Weldon Oct 31 '16 at 01:06
  • Well, i didn't write the explaination because I had writen it in the comment of original post. – Isukthar Nov 01 '16 at 22:36