在刷新页面之前等待UPDATE完成

时间:2014-05-30 15:43:45

标签: php mysql

我有一个HTML表单,用于更新MySQL中的表。不幸的是,我的页面重新加载了来自MySQL的预先更新的数据。有没有办法告诉PHP中的mysqli在重新加载(并再次执行SELECT)页面之前等待UPDATE完成?

谢谢!

(编辑:添加部分代码)

$mysql = mysqli_connect($host,$username,$password,$dbname);
$series_mysql = $mysql->query("SELECT Name, Path, Language FROM subtitles_config");
$series_list = array();
while($serie = mysqli_fetch_array($series_mysql))
    {
    array_push($series_list,array($serie["Name"],$serie["Path"],$serie["Language"]));
    }
sort($series_list);

if ($series_list != Null)
    {
    echo "<form action='?' method='post'><table><tr><th>Nom</th><th>Sous-titres</th></tr>\n\r";
    foreach ($series_list as $serie)
        {
        echo "<tr><td><input type=text size=40 readonly name='row[]' value='" . htmlspecialchars($serie[0], ENT_QUOTES) . "'><input type='hidden' name='row[]' value='" . htmlspecialchars($serie[1], ENT_QUOTES) . "'</td><td>";?>
            <select name='row[]'>
                 <option value="aucun"<?php if ($serie[2] == "aucun"): ?> selected="selected"<?php endif; ?>>aucun</option>
                 <option value="fr"<?php if ($serie[2] == "fr"): ?> selected="selected"<?php endif; ?>>fr</option>
                 <option value="en"<?php if ($serie[2] == "en"): ?> selected="selected"<?php endif; ?>>en</option>
                 <option value="fr,en"<?php if ($serie[2] == "fr,en"): ?> selected="selected"<?php endif; ?>>fr,en</option>
            </select><?php
        echo "</td></tr>\n\r";
        }
    echo "</table><input type='submit' name='update' value='Mettre à jour' /></form>\n\r";
    }

    if ( isset( $_POST['update']))
        {
        foreach (array_chunk($_POST['row'],3) as $serie)
            {
            $mysql->query("UPDATE subtitles_config SET Language='" . $serie[2] . "' WHERE Path='" . addslashes($serie[1]) . "'");
            }
        }

1 个答案:

答案 0 :(得分:0)

<?php
$mysql = mysqli_connect($host,$username,$password,$dbname);

if ( isset( $_POST['update'])) {
  foreach (array_chunk($_POST['row'],3) as $serie) {
    $mysql->query("UPDATE subtitles_config SET Language='" . $serie[2] . "' WHERE Path='" . addslashes($serie[1]) . "'");
  }
}

$series_mysql = $mysql->query("SELECT Name, Path, Language FROM subtitles_config");
$series_list = array();
while($serie = mysqli_fetch_array($series_mysql)) {
  array_push($series_list,array($serie["Name"],$serie["Path"],$serie["Language"]));
}

sort($series_list);

if ($series_list != Null) {
  echo "<form action='?' method='post'><table><tr><th>Nom</th><th>Sous-titres</th></tr>\n\r";

  foreach ($series_list as $serie) {
    echo "<tr><td><input type=text size=40 readonly name='row[]' value='" . htmlspecialchars($serie[0], ENT_QUOTES) . "'><input type='hidden' name='row[]' value='" . htmlspecialchars($serie[1], ENT_QUOTES) . "'</td><td>";
    ?>
    <select name='row[]'>
      <option value="aucun"<?php if ($serie[2] == "aucun"): ?> selected="selected"<?php endif; ?>>aucun</option>
      <option value="fr"<?php if ($serie[2] == "fr"): ?> selected="selected"<?php endif; ?>>fr</option>
      <option value="en"<?php if ($serie[2] == "en"): ?> selected="selected"<?php endif; ?>>en</option>
      <option value="fr,en"<?php if ($serie[2] == "fr,en"): ?> selected="selected"<?php endif; ?>>fr,en</option>
    </select>
    <?php
    echo "</td></tr>\n\r";
  }

  echo "</table><input type='submit' name='update' value='Mettre à jour' /></form>\n\r";
}

试试吧。

您的问题是,在您提取字段数据后,您正在更新所有内容。