M
M
Makfroy2016-06-17 13:04:13
PHP
Makfroy, 2016-06-17 13:04:13

Issue with Session php mysql entry?

The problem is this, there are several .phtml pages, on one of them a city is selected from the Mysql list, when selected, several fields with the name of the city change, but when I go to the main page, all fields are reset, what did I mess up? Did with $_SESSION['...'] = $_POST['...'];
Main page:

<?php
  session_start();

    ini_set('session.gc_maxlifetime', 3600*24*30);
    ini_set('session.cookie_lifetime', 3600*24*30);

  if(!$_SESSION['locality'])
  {
    $_SESSION['locality'] = $_POST['title'];
    }

?>
<html slick-uniqueid="3">
  <head>
<div class="location col-xs-2">
              <span class="">
                <img alt="#" src="<?=TEMP_DEF?>/images/location_icon.png">
                  <span id="simple_txt_n1"><a href="/citylist/">Ваш город: </a></span><a href="/citylist/" class="showMyCity"><?=$_SESSION['locality'] = $_POST['title'];?></a></span>
              </span>
            </div>

City selection page:
<?   unset($_SESSION['locality']);
session_destroy();
?>

<?
// указываем параметры для подключения к MySQL
$host='localhost'; // имя хоста
$database='...'; // имя базы данных
$user='...'; // заданное вами имя пользователя
$pswd='...'; // заданный вами пароль
// подключаемся к MySQL
$dbh = mysql_connect($host, $user, $pswd) or die("Не могу соединиться с MySQL.");
mysql_select_db($database) or die("Не могу подключиться к базе.");

mysql_set_charset('utf8');

function is_sel($a, $field)
{
  $b = isset($_POST[$field])?$_POST[$field]:NULL;
  if($a == $b) return 'selected="selected"';
}

$res = mysql_query('SELECT * FROM city');
?>
<div class="container">
  <form id="frm" method="post">
    <select name="region" onchange="document.getElementById('frm').submit()" >
      <option value='null'>- Выберите регион -</option>
      <?
      // выводим все строки из столбца name таблицы country
      while($row = mysql_fetch_array($res))
        echo "<option ".is_sel($row['id_record'],'region')." value='" . $row['id_record'] . "'>" . $row['region'] . "</option>\r\n";
      echo "</select>";

      if(isset($_POST['region']))
      {
        $res=mysql_query('SELECT * FROM city WHERE region="'.mysql_real_escape_string($_POST['region']).'"');

        if(mysql_num_rows($res)){

          echo "<select name=\"title\" onchange=\" document.getElementById('frm').submit();\" >
    <option  id=\"title\" value='0'>- Выберите pегион -</option>";

          while($row = mysql_fetch_array($res))
            echo "<option ".is_sel($row['id_record'],'title')." value='" . $row['id_record'] . "'>" . $row['title'] . "</option>\r\n";

          echo "</select>";

        }else{ // выбираю города без учета региона

          $res=mysql_query('SELECT * FROM city');

          echo "<select id=\"title\" name=\"title\" onchange=\"document.getElementById('frm').submit()\" >
    <option  value='0'>- Выберите город -</option>";

          while($row = mysql_fetch_array($res))
            echo "<option ".is_sel($row['id'],'title')." value='" . $row['id'] . "'>" . $row['title'] . "</option>\r\n";

          echo "</select>";

        }

      }

    
      ?>
  </form>
  <?
  $city = isset($_POST['title'])?$_POST['title']:NULL;
  if($city && $city !== 0)
  {
    $res=mysql_query('SELECT * FROM city WHERE id_record='.(int)$_POST['title'].' LIMIT 1');
    $row = mysql_fetch_array($res);
    echo "<h1>Ваш город ".$_SESSION['locality']= $row['title']."</h1>";
  }
  ?>
</div>

Tell me what's wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2016-06-17
@4Geka

It's just a mess of symbols, it's not clear at all because your eyes can be broken, and now you can't figure it out yourself, tk. the code is simply unreadable and it is impossible to understand where the tail is where the mane is. The code looks like it's from the mid 90s. no one has been using mysql_ for a long time, because depricated, no one will rake it up, rewrite normally if you want to achieve something in programming. Don't torture people with gkoda sheets like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question