php - 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");
};
?>
解决方案
欢迎唐 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.
})
请记住,如果此答案帮助您解决了您的问题,请选择它作为答案!
谢谢!
推荐阅读
- angular - 在Angular的子组件中替换父组件的元素
- matlab - 努力收敛到欧拉数
- regex - 使用 bash 脚本从字符串中提取多个匹配的子字符串
- python - 为什么按钮在我的代码中保持按下状态?
- java - 如何从命令行运行 Intellij 项目
- oracle - SQL DEVELOPER 中的 SELECT 语句
- php - PHP 致命错误:未捕获的错误:不能使用 stdClass 类型的对象作为数组
- r - 识别功能在R中不准确
- python - 无法从“termcolor”导入名称“colored”
- r - 如何使用服务器端逻辑切换dashboardHeader中的禁用(TRUE / FALSE)参数?