1

我正在用 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);
}
4

2 回答 2

2

听起来您可能正在将默认值设置为表单中的某些内容,这就是通过而不是新值获得的内容。

echo "<input type='hidden' name='user' value='$user' />";

也许,尝试不在表单中设置默认值,然后调试以查看为什么您的新值没有通过。

如果您发布更多代码,我们可能会提供更多帮助。

于 2012-07-13T18:51:03.977 回答
0

解决了:

# 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 个 $user 变量。进入表单的是最后一个显示在 html 表格中的表单。

于 2012-07-14T15:39:52.437 回答