简体   繁体   English

通过PHP插入/更新将详细信息更新到数据库,并在另一个页面中回显相同的内容,而无需刷新页面

[英]Update details to database through PHP insert/update and echo the same in another page without refreshing the page

I Have a php page which interact with DB to insert or update the details given in to the table. 我有一个php页面,它与DB进行交互以插入或更新表中给出的详细信息。

In user php page, user enters his details and so, when click on update, it leads to update php page, where it updates the details and then redirects to the same page. 在用户php页面中,用户输入他的详细信息,因此,当单击更新时,它会导致更新php页面,在该页面中,它会更新详细信息,然后重定向到同一页面。 But after it redirects to the user page, it doesn't echo the inserted/updated details until i refresh the page. 但是,在重定向到用户页面之后,它不会回显插入/更新的详细信息,直到我刷新页面为止。

the code goes as: 代码如下:

User page: 用户页面:

 <div class="col-sm-7 mgbt-xs-20">
    <h3 class="mgbt-xs-15 font-semibold"><i class="fa fa-file-text-o mgr-10 profile-icon"></i> ROOM BASIC DETAILS</h3>
      <div class="content-list content-menu">
         <ul class="list-wrapper">
            <li class="mgbt-xs-10"> <span class="menu-icon vd_green"><i class="fa  fa-circle-o"></i></span> \
              <span class="menu-text"> <a style="font-size:15px;"><?php echo ($row2['name']); ?></a> 
                at <a style="font-size:15px;" ><?php echo ($row2['location']); ?></a> 
                 <span class="menu-info"><span class="menu-date" style="font-size:15px;"> <?php echo ($row2['type']); ?></span><br>
                    <span class="menu-date" style="font-size:15px;"> <?php echo ($row2['star']); ?> Star </span></span> </span> 
             </li>
           </ul>
        </div>
   </div>

PHP Page: PHP页面:

$name = $_POST['name'];
$email = $_POST['email'];
$location = $_POST['location'];
$type = $_POST['type'];
$star = $_POST['star'];

$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE rooms SET name = '$name', location = '$location', type = '$type', star = '$star'
WHERE email = '$email'";

if ($conn->query($sql) === TRUE) {
echo '<script language="javascript">';
echo 'alert("Details have been Updated")';
echo '</script>';
echo '<a href="user.php"></a>';
}
else {
    echo "ERROR" . $sql . "<br>" . $conn->error;
}

Now as i told, the updated details won't show until i refresh the user page after it gets redirected from php page. 现在,正如我所说的那样,在从php页面重定向后刷新用户页面之前,不会显示更新的详细信息。

Please don't worry about the DB connection in php page, which i have n't mentioned in PHP code. 请不要担心php页面中的数据库连接,我在PHP代码中没有提到。

Any suggestions are appreciated.. 任何建议表示赞赏。

I did not see your code but I assumed that you placed your update query below the select query. 我没有看到您的代码,但我假设您将更新查询放置在选择查询下方。

Please move the code block of Update Query above the code block of Select Query. 请将“更新查询”的代码块移到“选择查询”的代码块上方。

In others words update should happen before your select query so that it fetches updated data. 换句话说,应该在选择查询之前进行更新,以便它获取更新的数据。 Otherwise, your select query will fetch data before updating & hence you need to refresh the page to see the updated data. 否则,您的选择查询将在更新之前获取数据,因此您需要刷新页面以查看更新的数据。

It should be like in the below order, 它应该像下面的顺序,

<?php
  include "dbconnection.php";
  include "update.php";

  //Perform Your select query here,
  $query = "SELECT * FROM table WHERE [YOUR CONDITION]";
  mysqli_query($con,$query); 
?>

  <!-- Your form goes here -->
 <form>
   <!-- submit this form to update user information -->
 </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM