0

the problem in storing option value from echo part into PHP variable

$var=$_POST[select_tag_name]

$query="SELECT subject.`SubjectName` 
        FROM `subject` 
            LEFT JOIN `class-sub-info` USING(SubjectId) 
        WHERE `ClassID`=$classid";

    $result=mysqli_query($con,$query);

    if(mysqli_num_rows($result) > 0) 
    {

        while($row=mysqli_fetch_assoc($result))
        {
            echo "<option value=".$row["SubjectId"].">".$row["SubjectName"]."</option>";
        }   

    }

expecting an easy way to store that value into a php variable

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 1
    Do you mean when the form is submitted? If so, where is the ` – RiggsFolly May 11 '19 at 16:09
  • Side note: you're vulnerable to [SQL injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –  May 11 '19 at 20:21

1 Answers1

0

Use Compensation Operator (.) to combine the output.

First create a variable like $result and assign empty value. after that use Compensation Operator to assign the output into variable.

 $var=$_POST[select_tag_name]
    //create a variable $result and assign empty value
    $subject= '';
    $query="SELECT subject.`SubjectName` 
            FROM `subject` 
                LEFT JOIN `class-sub-info` USING(SubjectId) 
            WHERE `ClassID`=$classid";

        $result=mysqli_query($con,$query);

        if(mysqli_num_rows($result) > 0) 
        {

            while($row=mysqli_fetch_assoc($result))
            { 
                //Use the Compensation Operator(.) to assign the output in variable
                $subject.="<option value=".$row["SubjectId"].">".$row["SubjectName"]."</option>";
            }   

        }

//At last display that variable
    echo $subject;
Shivendra Singh
  • 2,986
  • 1
  • 11
  • 11