【问题标题】:PHP variables remain blank after SQL fetch [closed]SQL获取后PHP变量保持空白[关闭]
【发布时间】:2020-06-04 23:39:48
【问题描述】:

我正在尝试创建一个自动填充信息按钮,该按钮从 MySQL 检索最后插入的信息,并与 $_GET 结合使用最新更新自动填充页面。我可以很好地插入和更新数据库,但是在检索到信息后,我的变量仍然为空。

我已确认正在发布项目填充按钮。检查查询时我也没有收到任何错误。

下面是不工作部分的 sn-p。它打印我的标题,但变量保持为空。

为草率的代码道歉。


if(isset($_POST['project-fill'])) {
  require "dbh.inc.php";

//select the last inserted row the one with the highest id  
  $proj1 = mysqli_query($conn, "SELECT * FROM updates WHERE id = MAX");
  $proj2 = mysqli_query($conn, "SELECT * FROM updates WHERE id = MAX");
  $proj3 = mysqli_query($conn, "SELECT * FROM updates WHERE id = MAX");

//select the column out of that row
  $result1 = mysqli_fetch_row($proj1['Project1']);
  $result2 = mysqli_fetch_row($proj2['Project2']);
  $result3 = mysqli_fetch_row($proj3['Project3']);

//transfer to project variable 
  $project1 = $result1;
  $project2 = $result2;
  $project3 = $result3;

//print header to allow for the $_GET method on the project page
  header (Location: ../update.php?update=sucessful&p1=".$project1."&p2=".$project2."&p3="$.project3);

} 

【问题讨论】:

  • 我没有看到你在呼应任何东西。另外,您不需要 3 个查询。你的header 也是错误的。
  • 我使用 $_GET 方法在另一个页面表单上回显该变量以从标题中检索它。这就是为什么我让标题脚本在检索每个相应变量的信息时更新标题。
  • 不要更改问题以匹配答案。那么没有人会理解我在答案中解决的问题。

标签: php html mysqli get


【解决方案1】:

这不是从结果中获取行的正确方法:

$result1 = mysqli_fetch_row($proj1['Project1']);

$proj1mysqli_result 对象,不是数组,所以不能下标。您需要将对象传递给mysqli_fetch_assoc(),它会返回一个关联数组,其中每一列都有一个元素。

$proj = mysqli_query($conn, "
    SELECT Project1, Project2, Project3 
    FROM updates 
    ORDER BY id DESC 
    LIMIT 1");
$row = mysqli_fetch_assoc($proj);
$project1 = $row['Project1'];
$project2 = $row['Project2'];
$project3 = $row['Project3'];

您无需查询 3 次即可从同一行获取不同的列。

【讨论】:

  • 所以我更正了从结果中获取行的方法(模仿显示的方法),不幸的是,标题仍然用空白值发布,好像变量返回空白并且在回显这个变量时什么都没有显示。我已成功插入值并在我的 phpmyadmin 中验证了这一点。是否要求此信息显示在表格中并且无法回显到表单内的输入值?
  • 启用error_reporting(E_ALL); 并检查您是否收到任何警告。
猜你喜欢
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
  • 2013-10-01
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
相关资源
最近更新 更多