Hello, I encountered such a problem: <select> displays information from the database, selecting it, we follow the link to the def. the page on which this select is also installed, but the first selection line is displayed again, and not the one (the name of the page) where we are at the moment, how can this be corrected?

Form Code:

 <form name="seria_name" method="POST" action="/template/seria_redirect.php">';?> <select name="seria_name" onchange="document.forms.seria_name.submit();return false"> <?php $query_seria = mysqli_query($dbc, "SELECT seria_name, id_seria FROM serias WHERE id_sezon = '$id_sezon'"); while ($row_seria = mysqli_fetch_array($query_seria)) { echo ' <option value="' . $row_seria['id_seria'] . '">' . $row_seria['seria_name'] . '</option>'; } echo '</select> </form>'; 

Handler Code:

 if (isset($_POST['seria_name'])) { $id_seria = mysqli_real_escape_string($dbc, trim($_POST['seria_name'])); $query_seria = mysqli_query($dbc, "SELECT id_sezon, id_serial FROM serias WHERE id_seria = '$id_seria'"); $row_seria = mysqli_fetch_array($query_seria); $id_sezon = $row_seria['id_sezon']; $id_serial = $row_seria['id_serial']; header('Location: /index.php?id=' . $id_serial . '&id_sezon=' . $id_sezon . '&id_seria=' . $id_seria); } 
  • Select the code as expected, it is impossible to read. Then talk about the problem - Barton
  • one
    position stored in variable =) - Gorets

2 answers 2

 <form name="seria_name" method="POST" action="/template/seria_redirect.php">';?> <select name="seria_name" onchange="document.forms.seria_name.submit();return false"> <?php $query_seria = mysqli_query($dbc, "SELECT seria_name, id_seria FROM serias WHERE id_sezon = '$id_sezon'"); $id_seria = (isset($_GET['id_seria']) && is_numeric($_GET['id_seria'])) ? $_GET['id_seria'] : 0; while ($row_seria = mysqli_fetch_array($query_seria)) { $selected=(!empty($id_seria) && $id_seria==$row_seria['id_seria']) ? 'selected="selected"' : ''; echo ' <option '.$selected.' value="' . $row_seria['id_seria'] . '">' . $row_seria['seria_name'] . '</option>'; } echo '</select> </form>'; 
  • swears at this line: $ id_seria = (isset ($ _ GET ['id_seria'] && is_numeric ($ _ GET ['id_seria'])))? $ _GET ['id_seria']: 0; syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' - ANTiK
  • no (not displayed in select ... just like it was - ANTiK
  • Omg, count the brackets))) $ id_seria = (isset ($ _ GET ['id_seria']) && is_numeric ($ _ GET ['id_seria']))? $ _GET ['id_seria']: 0; And moreover, in general $ _POST if it passes id_seria through the header ?? Well, you drive))) - Barton

Corrected the answer of the previous speaker.

  <form name="seria_name" method="POST" action="/template/seria_redirect.php"> <select name="seria_name" onchange="document.forms.seria_name.submit();return false"> <?php $query_seria = mysqli_query($dbc, "SELECT seria_name, id_seria FROM serias WHERE id_sezon = '$id_sezon'"); while ($row_seria = mysqli_fetch_array($query_seria)) { $selected = (isset($_GET['id_seria'])&&($_GET['id_seria']==$row_seria['id_seria']))? 'selected="selected"' : ''; echo ' <option value="' . $row_seria['id_seria'] . '" '.$selected.' >' . $row_seria['seria_name'] . '</option>'; } echo '</select> </form>'; ?> 
  • $selected = ($_POST['seria_name'] == $row_seria['id_seria']) 'selected="selected"' : ''; > syntax error, unexpected T_CONSTANT_ENCAPSED_STRING - ANTiK
  • $ _POST ['seria_name'] == $ row_seria ['id_seria']) this will never happen, the name and id are different things). And the bracket was forgotten by the way - Barton
  • lost ? after the condition - ikoolik
  • and this too - Barton
  • one
    @ANTiK, learn to look for a problem yourself. It was obviously written that there is a shortage of brackets or a comma, and the line is also indicated which one is not. If you don’t learn, it’s worthless to such a programmer who will write on the forum for every mistake)) - Barton