John Book John Book - 1 year ago 42
MySQL Question

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>';
$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].'';


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

echo '';

echo '</select>';

Answer Source

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].'';