【问题标题】:PHP/mysql/HTML - inserting string value and returning a displayPHP/mysql/HTML - 插入字符串值并返回显示
【发布时间】:2012-07-14 23:39:52
【问题描述】:

我正在将某个字符串值与 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);
}

【问题讨论】:

  • 想给我们看一下相关代码吗?
  • 我最好的猜测是检查正在创建的行的 ID。我们需要更多代码才能同时查看 select 和 insert 语句。
  • 是的,如果他只是将新值插入到数据库中,我们应该看看如何在数据库中搜索值..
  • 让我们看看插入和选择
  • 你可以通过打印你正在运行的sql来解决这个问题。我认为数据没有正确更新。

标签: php mysql html


【解决方案1】:

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

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

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

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

【讨论】:

  • 啊,现在可以了!这不是问题,是其他问​​题。我在 while 循环中显示结果,并且 $user 变量使用了两次,一次用于显示插入的数据,一次用于 sql 查询。我没有注意到名字是一样的。无论如何,tnx 寻求帮助
【解决方案2】:

已解决:

# 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 表格中的表单。

【讨论】:

    猜你喜欢
    • 2015-08-24
    • 2016-01-21
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2016-01-18
    • 2014-03-20
    • 2021-09-02
    • 2019-04-25
    相关资源
    最近更新 更多