我正在用 html 中的表单(它实际上是 php,我正在回显 html)将某个字符串值插入到 mysql 中。几天前,我插入了值让我们说“Ahhh”。现在,我正在尝试插入其他东西,但它仍然会带来那个旧值。这不是浏览器缓存,也不是我的代码中的值错误。
表格如下所示:
# form for adding a notice
echo "<form name='formAdd' action='controller.php' method='post'>";
echo "<input type='text' name='data' size='50'/>";
echo "<input type='hidden' name='user' value='$user' />";
echo "<input type='submit' name='submit' value='Add notice'/>";
echo "</form>";
我有 3 个 php 文件。functions.php、controller.php 和显示内容并创建您可以在上面看到的表单的主文件。我传递给 controller.php 的 $user 是应该插入的正确值(我打印它,没关系)。
在controller.php 我得到用户
if($_POST["user"]!=NULL){
# HERE he actually comes in, but gets another value from before.
$user = $_POST["user"];
}
调用插入函数后,我重定向回应该向我显示更新值的主文件,但它显示了错误的值。
有任何想法吗?
附加信息:
这会在主脚本中获得正确的用户。
# get username
$sql = "select username from user_auth where id=" . $_SESSION["sess_user_id"];
$result = mysql_query($sql) or die (mysql_error());
$name = mysql_fetch_array($result);
$user = $name['username'];
echo $user;
functions.php 看起来像:
#cacti DB specifications
$database = "cacti";
$hostname = "localhost";
$username = "xxxxxx";
$password = "xxxxxx";
$port = "xxxx";
mysql_connect($hostname,$username,$password);
mysql_select_db($database);
function addNotice($data,$user,$date){
$sql = "INSERT INTO lalalal (data,user,date) VALUES ('$data','$user','$date')";
$result = mysql_query($sql) or die (mysql_error());
}
function deleteNotice($id){
$sql = "DELETE FROM lalalala WHERE id='$id'";
$result = mysql_query($sql) or die (mysql_error());
}
controller.php 看起来像:
$user = NULL;
if($_POST["user"]!=NULL){
$user = $_POST["user"];
}
ini_set("display_errors", 1);
include_once("/var/www/html/cacti/plugins/lalala/functions.php");
$id = NULL;
$data = NULL;
$date = date('d.m.Y');
if($_POST["data"]!=NULL && $_POST["id"]==NULL){
$data = $_POST["data"];
addNotice($data,$user,$date);
}