【问题标题】:PHP variable wont show in form input fieldPHP变量不会显示在表单输入字段中
【发布时间】:2019-06-26 08:40:56
【问题描述】:

我正在尝试从数据库中选择一个 PHP 变量,并将其插入到 html 表单输入中。我想我的问题是如何将查询存储到变量中,然后以 html 形式调用该变量?此外,该表单位于与表单操作文件不同的页面上。如果它在 PHP 文件中定义,为什么它是未定义的?所需的输出是当我加载 html 页面时,来自数据库的昵称值会自动填充表单的该字段。

错误:
注意:未定义变量:C:\xampp\htdocs\Client-Projects\Crossfire\templates\CoinSubmission.html 中的昵称在 45 行b>

CoinSubmission.html

<form autocomplete="off" action="AdminCoinSub_Code.php" method="POST">
    <p>
        <input type="text" name="Nickname" id="Nickname" value="<?php echo htmlspecialchars($Nickname); ?>" />
    </p>
</form>

AdminCoinSub_Code.php

<?php {
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "administrator_logins";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO coin (ProfileID, Store, Position, 
        Nickname, ContactNumber, MachineCount, CutOffDate, Coins, location, LastSubmission, Rank) 
        VALUES (:ProfileID, :Store,:Position, :Nickname,:ContactNumber,:MachineCount,:CutOffDate, :Coins,:location,:LastSubmission,:Rank)");

    $stmt->bindParam(':ProfileID', $_POST['ProfileID']);
    $stmt->bindParam(':Store', $_POST['Store']);
    $stmt->bindParam(':Position', $_POST['Position']);
    $stmt->bindParam(':Nickname', $_POST['Nickname']);
    $stmt->bindParam(':ContactNumber', $_POST['ContactNumber']);
    $stmt->bindParam(':MachineCount', $_POST['MachineCount']);
    $stmt->bindParam(':CutOffDate', $_POST['CutOffDate']);
    $stmt->bindParam(':Coins', $_POST['Coins']);
    $stmt->bindParam(':location', $_POST['location']);
    $stmt->bindParam(':LastSubmission', $_POST['LastSubmission']);
    $stmt->bindParam(':Rank', $_POST['Rank']);

    $stmt->execute();

      echo "Success";
    }
catch(PDOException $e)
    {
      echo "Error: " . $e->getMessage();
    }
$conn = null;
}

$conn=mysqli_connect($servername,$username,$password,$dbname);
if (mysqli_connect_errno($conn))
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = "SELECT `Nickname` FROM `adminlogin` WHERE `ProfileID` = ':ProfileID'";
$Nickname = $conn->query($query);  // This is where the query is executed
$fetcher = $Nickname->fetch_assoc();
while($row = mysqli_fetch_array($Nickname))
if (mysqli_num_rows($Nickname) > 0) {
    echo 'User name exists in the table.';
} else {
    echo 'User name does not exist in the table.';
}

?>

【问题讨论】:

标签: php html mysql forms


【解决方案1】:

首先,你的 html 页面应该有扩展名 .php 而不是 .html,这样它就可以在 html 文件中解释你的 php 代码,不要'不用担心这不会破坏 html。

如果在php文件中定义了为什么是未定义的。

这是因为每个 php 脚本都是单独运行的,除非你将它们连接在一起。 我建议您阅读更多有关 php 工作原理的信息。

为了让这个例子起作用,我会这样做。

CoinSubmission.php

<?php //This goes at the top of the file
 include_once('AdminCoinSub_Code.php') //If they are in the same dir else you will need to set the path properly. 
?>

<form autocomplete="off" action="AdminCoinSub_Code.php" method="POST">
 <p>
  <input type="text" name="Nickname" id="Nickname" value="<?php echo 
   htmlspecialchars($Nickname); ?>">
 </p>
</form>

顶部的包含会将您的 AdminCoinSub_Code 代码“粘贴”到 CoinSubmission 文件中,并将其视为一个文件。所以这个变量是可以访问的。

注意:我的解释过于简单化了,并不完全是它的工作原理,但应该了解它的要点。

Alaa Morad 答案如果也有效,但记得将 .html 更改为 .php

快乐编码:)

【讨论】:

    【解决方案2】:

    那是因为如果 Register Globals 关闭,$Nickname 将不会被设置,这很正常!

    Register Globals 自 PHP 5.3.0 起已弃用,自 PHP 5.4.0 起已移除

    所以使用$_POST

    <input type="text" name="Nickname" id="Nickname" value="<?php echo htmlspecialchars($_POST['Nickname']); ?>">
    

    【讨论】:

    • @Dharman 对不起,我的意思是注册全局:en.wikibooks.org/wiki/PHP_Programming/…
    • Register globals 已从 PHP 中删除多年,没有理由提起它。让它安息吧。我仍然不认为这是他们问的问题。
    • 好的,但请记住,OP 正在将 POST 数据发送到一个文件,然后尝试在另一个文件中获取结果。这不是未注册全局变量的问题,而是对 PHP 变量如何工作的严重误解。
    猜你喜欢
    • 2017-06-12
    • 2023-03-12
    • 2020-09-05
    • 2022-06-10
    • 2020-07-06
    • 2014-06-12
    • 2015-08-20
    • 1970-01-01
    • 2011-09-17
    相关资源
    最近更新 更多