0

Good day folks,

Please I'm trying to implement something simple but I'm not finding the proper solution in PHP. I would like to ask for some advises, please.

I have one list of servers in the Array/Vector called $lines, now I would like to SELECT each server name in a MySQL DB in order to collect additional infos.

Could you please give me ideas regarding how to do a loop from the server names in, to SELECT / QUERY each one ?

Thanks!!

<html>
<head>

</head>
<body>

<form name="form1" method="post">
<textarea rows="10" name="servers[]" cols="50" ></textarea>
<input type="submit" name="submit" value="Submit" />
</form>

<?php
$con=mysqli_connect("hostxxxx","userxxxx","xxxxxxxx","xxxxxxx");
//Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  } 
if(isset($_POST["submit"]))
{
        if(!empty($_POST["servers"]))
        {
             echo '<h4>You have selected the following Servers:</4><br>';
            $submitted_array = array_keys($_POST["servers"]);                                                                  

       $lines = explode(PHP_EOL, $_POST["servers"][$submitted_array[0]]);
            foreach($lines as $servers)
            {           
        echo ($servers."<br>");
                }
}
else
{
echo 'Please write something';
}
}
?>
</body>
</html>
1
  • you can loop to gradually build up a string whose finished product will be a usable SQL statement (although beware of SQL injection vulnerabilities). You'd define the once-only parts of the SQL string outside the loop, and then the loop would add the parts which needed to add each server name. You would only need one SQL query, if you use an IN clause. Have you not made any attempt to do this at all? The concept is not too tricky. Commented May 2, 2018 at 20:18

1 Answer 1

1

I've got the result as I needed, I hope it can help:

<body>
<form name="form1" method="post">
<textarea rows="1000" name="servers[]" cols="100" style="width:300px;height:300px;"></textarea>
<input type="submit" name="submit" value="Submit" />
</form>

<?php
$con=mysqli_connect("hostxxxxx:portxx","userx","passxx","msdb");
//Check connection
if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }    
if(isset($_POST["submit"]))
{
   if(!empty($_POST["servers"]))
   {
           echo '<h4>You have selected the following Servers:</4><br>';
           $submitted_array = array_keys($_POST["servers"]);                                                                  

           //BREAKING IN AN ARRAY
    $lines = explode(PHP_EOL, $_POST["servers"][$submitted_array[0]]);
           //foreach($lines as $servers)
           //{            
        //echo ($servers."<br>");
           //}
//REMOVING BLANK SPACES:        
function trim_value(&$value) 
{
   $value = trim($value); 
}
//ADDING QUOTES BETWEEN THE VALUES
function add_quotes($str) {
   return sprintf("'%s'", $str);
}
//REMOVING BLANK LINES        
array_walk($lines, 'trim_value');        

$VCSAC2 = "VCSAC2";
$LPAR = "LPAR";
$HOSTF = "HOSTF";
$CLASSG = "CLASSG";
$IP = "IP";
$STATUS = "STATUS";


//USING IMPLODE        
//$sql=sprintf("SELECT * FROM main WHERE HOSTF IN (".implode(',', $lines).")");
$sql=sprintf("SELECT * FROM main WHERE HOSTF IN (".implode(',', array_map('add_quotes',$lines)).")");        
//USED TO CHECK SQL SINTAX        
//echo $sql;        

$result=mysqli_query($con,$sql);    
}
else
{
   echo 'Please write something';
}
}
?>

<table id="demo1" cellpadding="0" cellspacing="0" style="width:100%" style="font-size:13px">
        <thead>  
       <tr>
                   <th align=left>HostName</th>
                   <th align=left>VCS</th>
            <th align=left>LPAR</th>
            <th align=left>IP</th>
            <th align=left>Class</th>
        </tr>
        </thead>
           <?php while($dado = mysqli_fetch_array($result)) {?>

       <tbody>
       <tr>
        <td align=left style="font-size:14px"><?php echo $dado[$HOSTF]; ?></td>
        <td align=left style="font-size:14px"><?php echo $dado[$VCSAC2]; ?></td>
            <td align=left style="font-size:14px"><?php echo $dado[$LPAR]; ?></td>
            <td align=left style="font-size:14px"><?php echo $dado[$IP]; ?></td>
            <td align=left style="font-size:14px"><?php echo $dado[$CLASSG]; ?></td>
            </tr>
        </tbody>
           <?php } ?>
           </table> <br><br>
</body>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.