Search code examples
phparrayspostdata

How to get unique id from array implode when passing value through multiple selection php


How to get each value unique id from array implode when passing value through multiple selection php? When I pass single data, I can get the id; while when I pass multiple data it won't shown any id.

I have some ideas, is it the problem when select more that 2 values while post to next page; it will combine the data "ROBERT ALVIN" so the database could not catch the data; How do I split the data to "ROBERT" "ALVIN", so I would able to get the id "1" "2".

when select single data!

http://b62i.imgup.net/33ba8c.png

when select two or more data!

http://v58i.imgup.net/1232cde.png

Sample coding.

$myselected     =   $_POST["auditor"];
$auditor = implode (" ",$myselected);

$query10 = "SELECT * FROM auditor WHERE auditor_name = '$auditor' ";
$result10 = $db->query($query10);
$row10 = $result10->fetch_array();

<tr>
<td><b>Auditor:</b></td>

<td colspan='5'>
<?php 
echo'<input type="text" name="form_details_id" value="'.$row10["id"].'">';

    foreach ($myselected as $auditor){ 
    echo $auditor."<br>\n"; 
    }
?>
</td>

Solution

  • to match a against multiple values you can use the mysql IN clause. you create a string of the values in quotes useing implode

    $myselected     =   $_POST["auditor"];
    
    if(count($myselected)>1){
    $auditor = implode ("','",$myselected);
    }else
    $auditor =$myselected;
    }
    
    $query10 = "SELECT * FROM auditor WHERE auditor_name IN ('$auditor') ";
    

    the rest is as you have above. You need to add user input sanitation to the above