我在mysql中插入一个带有html格式的字符串值(实际上是php,我在回显html)。几天前,我插入了一个值,让我们说"啊"。现在,我试图插入其他内容,但它仍然会带来旧的价值。这不是浏览器缓存,也不是我代码中的值错误。
表单如下:
# 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"];
}
调用insert函数后,我重定向回主文件,该文件应该显示更新后的值,但它显示的却是错误的值。
有什么想法吗?
附加信息:
这将在主脚本中获得正确的用户。
# 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);
}
听起来你可能正在将默认值设置为表单中的某个值,而这正是它的作用,而不是新值。
echo "<input type='hidden' name='user' value='$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;
# get data
$sql = "SELECT * FROM lalala";
$result = mysql_query($sql) or die (mysql_error());
echo "<table CELLPADDING=2 border=1 align=center>";
echo "<tr>";
echo "<th>User</th>";
echo "<th>Notice</th>";
echo "<th>Date</th>";
echo "</tr>";
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$user = $row['user'];
$data = $row['data'];
$date = $row['date'];
echo "<tr>";
echo "<td>".$user."</td>";
echo "<td>".$data."</td>";
echo "<td>".$date."</td>";
echo "<td><form name='formDelete' action='controller.php' method='post'>";
echo "<input type='hidden' name='id' value='$id' />";
echo "<input type='submit' name='submit' value='Delete'/>";
echo "</form></td>";
echo "</tr>";
}
echo "</table><br>";
# 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>";
问题是存在2$用户变量。进入表单的是html表中显示的最后一个。