首页 > 解决方案 > Mysql 数据库更新,但即使我刷新,我在 php html 中的表也不会更新

问题描述

我有搜索我的字段名称(familycode)然后显示一个表的代码。我将代码包含在表中以更新表和数据库。

我的问题 - 更新后 - 我的显示信息不会更新,也不会刷新或显示来自 mysql 数据库的正确数据。数据库已更新。我是新手,这是第一篇文章。我尝试了许多不同的方法,但没有运气。我会很感激一些想法!谢谢!

我的主页代码 d7.php

<?php

// connection
$dbcon= NEW Mysqli("localhost", "root", "", "xxx");

if (!$dbcon) {
echo " ----------Error connecting to database--------------";
}
else {   
echo " ----------Connected to Database Successfully----------- <br>" ; 
}


include 'updatedata.php';
?>
<html>
<head>
<title> View_Update Family</title>
<link rel="stylesheet" type="text/css" href="d7css.css"> 
</head> 
<body> 
        <form action=d7.php method=post>
        <input type="text" name="valueToSearchfamily" placeholder="Family To 
Search"><br><br>
        <input type="submit" name="searchfamily" value="Search Family"><br> 
<br>            
        </form>
<?php

if(isset($_POST['searchfamily']))
{
$valueToSearchfamily=$_POST['valueToSearchfamily'];
// search in all table columns

$select = "SELECT * FROM families WHERE Familycode LIKE 
'%".$valueToSearchfamily."%' ";
$mydata=mysqli_query($dbcon, $select);        
}
else {
$notselect = "SELECT * FROM families ORDER BY Familycode ";
$mydata=mysqli_query($dbcon, $notselect);
}

echo "<table class='updatetable' >";
echo "<tr>
                <th>Id</th>
                <th>Familycode</th>
                <th>Name</th>
                <th>Street</th>
                <th>City</th>
                <th>State</th>
                <th>Zip</th>              
</tr>";
                while($record = mysqli_fetch_array($mydata)){

        echo "<form action=updatedata.php method=post>";
        echo "<tr>";
        echo "<td>" . "<input type=text name=aid value=" . $record['Aid'] . 
  " </td>";
        echo "<td>" . "<input type=text name=familycode value=" . 
  $record['Familycode'] . " </td>";     
        echo "<td>" . "<input type=text name=name_mailing value=" . 
  $record['Name_mailing'] . " </td>";       
        echo "<td>" . "<input type=text name=street_mailing value=" . 
  $record['Street_mailing'] . " </td>";
        echo "<td>" . "<input type=text name=city_mailing value=" . 
  $record['City_mailing'] . " </td>";
        echo "<td>" . "<input type=text name=st_mailing value=" . 
  $record['St_mailing'] . " </td>";
        echo "<td>" . "<input type=text name=zip_mailing value=" . 
  $record['Zip_mailing'] . " </td>";        
        echo "<td>" . "<input type=hidden name=hidden value=" . 
  $record['Aid'] . " </td>";
        echo "<td>" . "<input type=submit  name=update value=update" . " 
  </td>";       
        echo "</form>";        
  };
  ?>

  </body>
  </html>

  my other file as I tried separating them is updatedata.php

  <?php
 // connection
 $dbcon= NEW Mysqli("localhost", "root", "", "xxx");
 if (!$dbcon) {
 echo " ----------Error connecting to database--------------";
 }
 else {   
 echo " ----------Connected to Database Successfully----------- <br>" ; 
 }
 // ==========================================================

 if(isset($_POST['update'])) {
 $updatequery="UPDATE families SET Aid='$_POST[aid]', 
 Familycode='$_POST[familycode]', Name_mailing='$_POST[name_mailing]', 
 Street_mailing='$_POST[street_mailing]', 
 City_mailing='$_POST[city_mailing]', St_mailing='$_POST[st_mailing]', 
 Zip_mailing='$_POST[zip_mailing]' WHERE Aid='$_POST[hidden]'";

  mysqli_query($dbcon, $updatequery);

  header("location: index_dir.php");
  };
  ?>

标签: phpdatabase

解决方案


欢迎唐 T 使用 stackoverflow!似乎您在一个实例中回显到页面上,回想一下,当您回显某些内容时,当它在数据库内部发生更改时它不会改变,因为您没有再次进行调用,它在同一页面上。要解决这个问题,您需要使用 Javascript + Ajax,我建议使用 jQuery 并利用其基础知识从 PHP 页面发布和获取数据。这是 jQuery Ajax 的链接:http: //api.jquery.com/jquery.ajax/

使用 jQuery ajax 的简单示例:

$.ajax({
   url: 'PHP SCRIPT PAGE URL',
   method: 'POST', // Your sending data, use POST
   data: 'Your Data!', // This can be also be a form object
   success: function(response) { // Response is your PHP scripts answer to this request.
      console.log(response);     // You may Echo or use JSON format as a response.
   }                             // If you use JSON, add dataType: 'JSON' in the ajax call to the left.
})

请记住,如果此答案帮助您解决了您的问题,请选择它作为答案!

谢谢!


推荐阅读