How to use Array[] in where cluase and replace it with database value

hi
I have following html form where i can add mutiple row as well for new enteries

<form>
<input type  = text  name = name[] id = name >
<input type  = text  name = rollno[] id = rollno >
<input type  = submit  value = submit > </form>

After submit i want to display this form value via post method as

<?php

if(isset($_POST['submit']))
{
$name = $_POST['name'];
$rollno = $_POST['rollno'];
}

Now I want to replace $_POST[‘rollno’]; with fathername field value available in mysql table ‘student’ as follow

 $sql = "SELECT fname FROM student WHERE 'rollno' = '$rollno'";
          $result = $con->query($sql); 
    if ($result->num_rows > 0) {       

        while ($row = $result->fetch_assoc()) {

                $fname = $row['fname'];
		
}}

currently its given as : Notice: Array to string conversion in /home/thehospi/public_html/le/bill/invoice.php on line 61

my html form using array as multiple entries can be possible with one submit, Guidance needed regarding this

You must decide which of the values of $rollno you want to use in the query - you can’t just stick an array into a query like that.

If you want to search for multiple values, you must either build up a query using OR or AND clauses depending on which you want, or maybe the IN clause would be more suitable.

whata about for loop as i could have multiple post value, can look fix that issue ?

What, you mean run the query multiple times in a for() loop, and then just display the results? That would probably work, but it would be easier to run one query for all the values. It sort-of depends on what you’re going to do next in the code.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.