Adrian Prime
Adrian Prime

Reputation: 39

Insert to database information with select option php

I try to insert some information to database i need to select and option then to remember the last option so i don't select all the time same option

The problem with me code is it remembers the last loop.

<form action="" method="post">

<select name="db_tablelist" onchange="showTables(this.value)">
 <option value="">Select a table:</option>';
 <?php
 $result = mysqli_query($con,"SHOW TABLES");
 while($row = mysqli_fetch_array($result)) { 

 if (isset($_POST['db_tablelist']) == $row[0]){

    echo '<option  value="' . $row[0] . '" selected="selected" >'.$row[0].''; 

} 

else
{
    echo '<option  value="' . $row[0] . '">'.$row[0].''; 

       echo '';
}

}
 echo '</select>';
 ?>

Upvotes: 0

Views: 48

Answers (2)

PoorDeveloper
PoorDeveloper

Reputation: 186

I think the problem is here: if (isset($_POST['db_tablelist']) == $row[0]) You're checking if isset result equals to $row[0]

Upvotes: 0

Tanveer Hussain
Tanveer Hussain

Reputation: 123

Your code has problem when you are mapping post data with db data. You are checking post with isset function but you are also mapping immediately. So you have add one more condition with & operator.

This should be work.

if (isset($_POST['db_tablelist']) and $_POST['db_tablelist'] == $row[0]){

  echo '<option  value="' . $row[0] . '" selected="selected" >'.$row[0].''; 

}   

Upvotes: 1

Related Questions